jQuery Minimalectのonchange
selectタグの見た目をカスタマイズできるようにするMinimalectですが、onchangeが独自イベントになっており、selectのonchangeが発生しません。
これだと困ることがあるので、Minimalectのonchangeでselectのonchangeを呼び出すようにします。
$('select').each(function() { $(this).minimalect({ source: this, onchange: function() { $(this.source).trigger('change'); } }); });
テキストボックスへの貼り付けを禁止するjQueryプラグイン
(function($) { $.fn.noPaste = function() { this.bind('paste contextmenu', function() { return false; }).bind('keydown', function(event) { if ((event.keyCode == 86 && event.ctrlKey) || (event.keyCode == 45 && event.shiftKey)) { return false; } }); }; })(jQuery);
使い方は以下の通り。
<input type="text" name="mail_address_verify" value="" class="verify" /> <script type="text/javascript"> $(document).ready(function() { $(".verify").noPaste(); }); </script>
多言語化時の検索結果を表示中の言語のノードのみにする
hook_query_alter()でがんばる。
<?php function mymodule_query_alter(QueryAlterableInterface $query) { if ($query->hasTag('node_access') && $query->hasTag('pager')) { global $language; $tables = $query->getTables(); foreach ($tables as $table) { if ($table['table'] == 'node') { $query->condition('n.language', $language->language, '='); } } } }
hookの実行順を変更する
hook_module_implements_alter()を実装する。
下記はhook_cron()を最後に実行させる場合の例。
<?php function mymodule_module_implements_alter(&$implementations, $hook) { if ($hook == 'cron') { $group = $implementations['mymodule']; unset($implementations['mymodule']); $implementations['mymodule'] = $group; } }
イベントハンドラを使う
下記はViewのbeforeRenderで何かしたい場合の例。
イベント発生前にハンドラを追加すればいいので、beforeRender()でなくても可。
<?php class SampleController extends AppController { public function beforeRender() { $this->getEventManager()->attach(array($this, 'beforeRenderView'), 'View.beforeRender', array()); } /** * @param CakeEvent $e */ public function beforeRenderView($e) { echo $e->data[0]; } }
自前のイベントを呼び出したいときは
$this->getEventManager()->dispatch('SampleController.beforeRedirect');
のようにする。
render()後のHTMLを取得して変更する
Controllerのrender()をオーバーライドする。
全体のHTMLに対してやるならこれが一番かなと。
下記は全角英数字を半角に一括変換する例。
public function render($view = null, $layout = null) { $response = parent::render($view, $layout); $content = $response->body(); $content = mb_convert_kana($content, 'a'); $response->body($content); return $response; }