Code http://drupal8.ovh/en/tags/code en Redirect a user after login. Redirection on Drupal 8. http://drupal8.ovh/en/tutoriels/310/redirect-a-user-after-login-redirection-on-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Redirect a user after login. Redirection on Drupal 8.</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to redirect a user once logged on (on Drupal 8) ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Tue, 05/23/2017 - 10:15</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>To make a redirection after user login, we can use the hook hook_user_login().<br /> Note : You cannot use $form_state-&gt;setRedirectUrl() directly in the form alter, since it will be overwritten by UserForm::submitForm().</p> <p><code>Example : We want to redirect a user to the page "mymodule.dashboard".<br /> /**<br />  * Implements hook_user_login().<br />  */<br /> function mymodule_user_login(\Drupal\user\UserInterface $account) {<br />   // Default login destination to the dashboard.<br />   $current_request = \Drupal::service('request_stack')-&gt;getCurrentRequest();<br />   if (!$current_request-&gt;query-&gt;get('destination')) {<br />     $current_request-&gt;query-&gt;set(<br />       'destination',<br />       \Drupal\Core\Url::fromRoute('mymodule.dashboard')-&gt;toString()<br />     );<br />   }<br /> }</code></p> <p> Tips : Page redirection. Redirect on a basic controller :</p> <p><code>$url = \Drupal\Core\Url::fromRoute('mymodule.DESTINATION_PAGE');<br /> return new \Symfony\Component\HttpFoundation\RedirectResponse($url);</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/module" hreflang="en">Module</a></li> <li><a href="/en/tags/user" hreflang="en">User</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/105/form-redirection-on-submit" hreflang="en">Form redirection on submit</a></div> <div class="field__item"><a href="/en/tutoriels/88/page-redirection-on-drupal-8" hreflang="en">Page Redirection on Drupal 8</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=310&amp;2=comment&amp;3=comment" token="MpTjJmsqT33l95fy_QwC7O77eNFif0Bz6JEdAHBm7ag"></drupal-render-placeholder> </section> Tue, 23 May 2017 08:15:28 +0000 editor 310 at http://drupal8.ovh Git Tags http://drupal8.ovh/en/tutoriels/308/git-tags <span class="field field--name-title field--type-string field--label-hidden">Git Tags</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to use tags of git. add remove git tag ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mon, 05/15/2017 - 21:31</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Add remove git tags.</h2> <h3>Create a tag</h3> <p>git tag &lt;TAGNAME&gt;</p> <h3>Push tag</h3> <p>git push origin &lt;TAGNAME&gt;<br /> git push --tags</p> <h3>Pull tags</h3> <p>git tag -l<br /> git checkout tags/&lt;TAGNAME&gt;</p> <h3>Delete local tag</h3> <p>git tag -d &lt;TAGNAME&gt;</p> <h3> Delete remote tag</h3> <p>git push origin :&lt;TAGNAME&gt;</p> <h3>Show tag list</h3> <p>git tag</p> <h3> Show selected tag list</h3> <p>git tag -l "TAGNAME*"</p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/git" hreflang="en">Git</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/11/helpful-git-commands" hreflang="en">Helpful git commands</a></div> <div class="field__item"><a href="/en/tutoriels/96/git-create-new-branche-push-and-merge" hreflang="en">Git - Create new branche, push and merge</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=308&amp;2=comment&amp;3=comment" token="Par00chuEhm196iJ7CUEJtHG0NPg66dgUgSGqp66YHs"></drupal-render-placeholder> </section> Mon, 15 May 2017 19:31:44 +0000 editor 308 at http://drupal8.ovh YAML or JSON http://drupal8.ovh/en/tutoriels/307/yaml-or-json <span class="field field--name-title field--type-string field--label-hidden">YAML or JSON</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>Is it preferable to use YAML or JSON on drupal 8 ? What are the differents ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Thu, 05/11/2017 - 13:42</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Yml vs Json</h2> <p>A make a small test to test YAML vs PHP performance. The result is:</p> <table><tbody><tr><td> </td> <td>YAML</td> <td>JSON</td> </tr><tr><td>Encode 30K, x100</td> <td>3s</td> <td>0s</td> </tr><tr><td>Encode 30K, x1000</td> <td>27s</td> <td>1s</td> </tr><tr><td>Decode  30K, x100</td> <td>14s</td> <td>0s</td> </tr><tr><td>Decode  30K, x1000</td> <td>142s</td> <td>2s</td> </tr><tr><td>File Size</td> <td>30K</td> <td>21K</td> </tr></tbody></table><p>   </p> <p>I think no need to explain the result !!!</p> <p>The Test:<br /><code>$file = 'views.view.files.yml';//30KB<br /> $file_yml = file_get_contents($file);<br /> $file_json = file_get_contents($file . '.json');<br /> //Prepare<br /> $data = \Drupal\Component\Serialization\Yaml::decode($file_yml);<br /> //file_put_contents($file . '.json', \Drupal\Component\Serialization\Json::encode($data));<br /> $loops = 1000;<br /> $time = time();<br /> for ($i = 0; $i &lt; $loops; $i++) {<br /> //Decode test<br /> //$temp = \Drupal\Component\Serialization\Yaml::decode($file_yml);<br /> //$temp = \Drupal\Component\Serialization\Json::decode($file_json);<br /> //Encode test<br /> //$temp = \Drupal\Component\Serialization\Yaml::encode($data);<br /> //$temp = \Drupal\Component\Serialization\Json::encode($data);<br /> //Check<br /> //print_r($temp); break;<br /> }<br /> $time = time() - $time;<br /> echo "Time : $time sec\n";</code></p> <p><strong>Nota : On Yaml (.yml) you can appen data without reading (You can't do with JSON). Example:</strong></p> <p><code>$yml = Yaml::encode([$yml_data]);<br /> file_put_contents('file_name.yml', $yml, FILE_APPEND);</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/json" hreflang="en">JSON</a></li> <li><a href="/en/tags/yaml" hreflang="en">YAML</a></li> </ul> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=307&amp;2=comment&amp;3=comment" token="Y6C6rDUbh7mZFTki19bcRrQvXBOTp4GKIlLz3lsgWJc"></drupal-render-placeholder> </section> Thu, 11 May 2017 11:42:12 +0000 editor 307 at http://drupal8.ovh Drupal 8 Cache contexts http://drupal8.ovh/en/tutoriels/303/drupal-8-cache-contexts <span class="field field--name-title field--type-string field--label-hidden">Drupal 8 Cache contexts</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to use drupal 8 cache contexts ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mon, 04/24/2017 - 13:00</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Drupal 8 shiped with a powerfull cache API able to set cache according to a context. correct usage of this cache improve the site preformence greatly.</p> <p>https://www.drupal.org/docs/8/api/cache-api/cache-contexts<br /> https://www.drupal.org/node/2451661</p> <h2> Cache contexts</h2> <p><code>cookies:NAME<br /> headers:NAME<br /> ip<br /> languages:TYPE<br /> request_format<br /> route.book_navigation<br /> route.menu_active_trails:MENU_NAME<br /> route.name<br /> session.exists<br /> theme<br /> timezone<br /> url.path.is_front<br /> url.path.parent<br /> url.query_args:KEY<br /> url.query_args.pagers:PAGER_ID<br /> url.site<br /> user.is_super_user<br /> user.node_grants:OPERATIONS<br /> user.permissions<br /> user.roles:ROLE</code></p> <p> </p> <p>Examples :</p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/cache" hreflang="en">Cache</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/256/drupal-8-cache-api" hreflang="en">Drupal 8 Cache API</a></div> <div class="field__item"><a href="/en/tutoriels/155/drupal-8-tags-based-cache-system" hreflang="en">Drupal 8 Tags based cache system</a></div> <div class="field__item"><a href="/en/tutoriels/158/cache-drupal-8-render-arrays" hreflang="en">Cache Drupal 8 Render Arrays</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title">Comments</h2> <a id="comment-178"></a> <article role="article" data-comment-user-id="0" about="/en/comment/178" typeof="schema:Comment" class="comment js-comment by-anonymous clearfix"> <span class="hidden" data-comment-timestamp="1494499142"></span> <footer class="comment__meta"> <article typeof="schema:Person" about="/user/0" class="profile"> </article> <p class="comment__author"><span rel="schema:author"><span lang="" typeof="schema:Person" property="schema:name" datatype="">keltan (not verified)</span></span> </p> <p class="comment__time">Thu, 05/11/2017 - 12:39 <span property="schema:dateCreated" content="2017-05-11T10:39:02+00:00" class="rdf-meta hidden"></span> </p> <p class="comment__permalink"><a href="/en/comment/178#comment-178" hreflang="en">Permalink</a></p> </footer> <div class="comment__content"> <h3 property="schema:name" datatype=""><a href="/en/comment/178#comment-178" class="permalink" rel="bookmark" hreflang="en">Great Post</a></h3> <div property="schema:text" class="clearfix text-formatted field field--name-comment-body field--type-text-long field--label-hidden field__item"><p>I'm very inspired with this weblog. this indicates it literally does not have any dietary cost and consuming it virtually makes your body use up stored nutrients to method it. last time I visit <strong><a href="https://essayjaguar.com/">write my essay</a></strong> this website also awesome Honey then again has many useful nutrients, enzymes, and antioxidants; uncooked honey is first-class as standard honey processing</p> <p> </p> <p> </p> </div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=178&amp;1=default&amp;2=en&amp;3=" token="XQgKwfX3-yg3a2HPd8sa-EK3cL54WqjHQW8gjIB9lME"></drupal-render-placeholder> </div> </article> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=303&amp;2=comment&amp;3=comment" token="-YtvT33Jx9xHayvNE42Im_2YOoM4mnIrG9c6boHP6VU"></drupal-render-placeholder> </section> Mon, 24 Apr 2017 11:00:38 +0000 editor 303 at http://drupal8.ovh Memcache - Install memcached on linux and Drupal. http://drupal8.ovh/en/tutoriels/302/memcache-install-memcached-on-linux-and-drupal <span class="field field--name-title field--type-string field--label-hidden">Memcache - Install memcached on linux and Drupal.</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to install and configure memcached on debian 8 linux (or similar like ubuntu ...)  and setup with drupal 8 ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mon, 04/24/2017 - 11:43</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Memcache is caching server allow you to incrise and have great performence on your site. Drupal and memcached work very well and here the full tutorial you need to install and setup memcache server and your drupal instance.</p> <h2>Install Mamcache</h2> <p><code>sudo apt-get install memcached</code></p> <h2>Start and get details</h2> <p><code>#Start<br /> service memcached start<br /> #Get details<br /> #01<br /> service memcached status<br /> #02<br /> ps -eaf | grep memcached<br /> #03<br /> netstat -tap | grep memcached<br /> #04 : Get full details<br /> echo "stats settings" | nc localhost 11211</code></p> <p> </p> <h2>Memcache configuration</h2> <p>Memcached configuration file is located on :  /etc/memcached.conf<br /> You can set the port, set memory size .... on this file.<br />     </p> <h2>Install PECL memcache packages</h2> <p>Chack :<br /> php -m |grep mem<br /> Install: (Not tested)<br /> /usr/local/bin/pecl install memcache<br /> Link : https://www.servint.net/university/article/the-tech-bench-how-to-install-php-memcache/</p> <h2> </h2> <h2>Setup Drupal tu use memcached instead of the database.</h2> <p>Edit the settings.php and add m configuration.</p> <p><code>//Memcache configuration<br /> $settings['memcache']['servers'] = ['127.0.0.1:11211' =&gt; 'default'];<br /> $settings['memcache']['bins'] = ['default' =&gt; 'default'];<br /> $settings['memcache']['key_prefix'] = '';</code></p> <p><code>$settings['cache']['default'] = 'cache.backend.memcache';</code></p> <p> </p> <h2>How to check drupal using memcached not the database ?</h2> <p>1. TRUNCATE all tables start with cache_<br /> 2. Clear the cache using 'drush cr' or via interface.<br /> -&gt; If the tables start with cache_ does not contains any records, memcache is configured correctly. otherwise re check the configuration.<br /> NOTE : Even correctly configured, Some times you can see 1 or two records in some tables. (memcache module is currently in development (Alpha version (2017/04/24)))</p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/cache" hreflang="en">Cache</a></li> <li><a href="/en/tags/server" hreflang="en">Server</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/256/drupal-8-cache-api" hreflang="en">Drupal 8 Cache API</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=302&amp;2=comment&amp;3=comment" token="2vTa-Y7gHTt3pZV7_hgfeuGgfVzxoPPLcB7tzoo0J58"></drupal-render-placeholder> </section> Mon, 24 Apr 2017 09:43:08 +0000 editor 302 at http://drupal8.ovh Few Tips to create a custom module http://drupal8.ovh/en/tutoriels/300/few-tips-to-create-a-custom-module <span class="field field--name-title field--type-string field--label-hidden">Few Tips to create a custom module</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to Check module exist ?<br /> How to Get module / Theme install directory ?<br /> How to Get a file in a custom module</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Wed, 03/29/2017 - 16:58</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p> </p> <h3>Check module exist</h3> <p><code>if (\Drupal::moduleHandler()-&gt;moduleExists('my_module')) {<br />     $message = 'Module exist.';<br /> }<br /> else {<br />     $message = 'Not exists.';<br /> }</code><br />  </p> <h3>Get module / Theme install directory</h3> <p><code>$path = drupal_get_path('module', 'my_module');</code></p> <h3>Get a file in a custom module</h3> <p><code>$path = drupal_get_path('module', 'my_module') . '/my_module.info.yml';</code></p> <p> </p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/module" hreflang="en">Module</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/3/create-a-simple-module-for-drupal-8" hreflang="en">Create a simple module for Drupal 8 - Step 1</a></div> <div class="field__item"><a href="/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page" hreflang="en">Create a simple module for Drupal 8 - Step 2 - Create Simple Page</a></div> <div class="field__item"><a href="/en/tutoriels/12/create-a-simple-module-for-drupal-8-step-3-create-dynamic-page" hreflang="en">Create a simple module for Drupal 8 - Step 3 - Create Dynamic Page/ Block</a></div> <div class="field__item"><a href="/en/tutoriels/283/create-a-field-a-node-entity-programmatically-on-drupal-8" hreflang="en">Create a field in a node entity programmatically on Drupal 8.</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=300&amp;2=comment&amp;3=comment" token="010dPsm-DdBKGjUYFZkXmYBCTUy3W_76IA_L7ReUE6o"></drupal-render-placeholder> </section> Wed, 29 Mar 2017 14:58:49 +0000 editor 300 at http://drupal8.ovh Avoid Spam form submissions - (Easy way of form Anti-Spam) http://drupal8.ovh/en/tutoriels/296/avoid-spam-form-submissions-easy-way-of-form-anti-spam <span class="field field--name-title field--type-string field--label-hidden">Avoid Spam form submissions - (Easy way of form Anti-Spam)</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to avoid spam contact form or other form submissions by robots (spam-bots) ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Thu, 03/16/2017 - 10:14</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p><strong>Method 1. (Classical way)</strong></p> <p>Use a captcha system like Math captcha, Image captcha, reCaptcha of google....</p> <h2>Method 2. Use Honeypot</h2> <p>Honeypot use a very simple method to limit unwanted form submission by robots (any way, some robots can pass through this method). This method can block up to 98% of spam form submissions.</p> <h3>Concept of Honeypot.</h3> <p>By adding an invisible field to your forms that only spambots can see, you can trick them into revealing that they are spam bots and not actual end-users.</p> <p>Here we have a simple checkbox that:</p> <ul><li>Is hidden with CSS.</li> <li>Has an obscure but obviously fake name.</li> <li>Has a default value equivalent 0.</li> <li>Can't be filled by auto-complete</li> <li>Can't be navigated to via the Tab key. (See tabindex)</li> </ul><h3>Install / Implement Honeypot on drupal.</h3> <p>Just install and configure drupal Honeypot contrib module.</p> <h3><br /> Install / Implement Honeypot on custom PHP Website.</h3> <h4>HTML Code</h4> <p><code>&lt;input type="checkbox" name="contact_me_by_fax_only" value="1" style="display:none !important" tabindex="-1" autocomplete="off"&gt;</code></p> <p> </p> <h4>Server-Side</h4> <p>On the server side we want to check to see if the value exists and has a value other than 0, and if so handle it appropriately. This includes logging the attempt and all the submitted fields.</p> <p>In PHP it might look something like this:</p> <p><code>$honeypot = FALSE;<br /> if (!empty($_REQUEST['contact_me_by_fax_only']) &amp;&amp; (bool) $_REQUEST['contact_me_by_fax_only'] == TRUE) {<br />     $honeypot = TRUE;<br />     log_spambot($_REQUEST);<br />     // treat as spambot<br /> } else {<br />     // process as normal<br /> }</code></p> <h2><br /> An Example of Honeypot php class for easy implementation.</h2> <h3>PHP Class</h3> <p><code>&lt;?php<br /><br /> class Honeypot {<br /><br />   const FIELD_NAME = "contact_me_by_fax_only";<br /><br />   /**<br />    * Get Honeypot form input field.<br />    * @return string<br />    */<br />   public static function getHoneypot() {<br />     $field_name = self::FIELD_NAME;<br />     $form_input = '&lt;input type="checkbox" name="' . $field_name . '" value="1" style="display:none !important" tabindex="-1" autocomplete="off"&gt;';<br />     return $form_input;<br />   }<br /><br />   /**<br />    * Check the form submission.<br />    * @return bool<br />    */<br />   public static function checkHoneypot() {<br />     $honeypot = FALSE;<br />     $field_name = self::FIELD_NAME;<br />     if (!empty($_REQUEST[$field_name]) &amp;&amp; (bool) $_REQUEST[$field_name] == TRUE) {<br />       $honeypot = TRUE;<br />     }<br />     return $honeypot;<br />   }<br /> }</code></p> <p> </p> <h3>Usage:</h3> <h4>At the form creation:</h4> <p><code>&lt;form&gt;<br />     ...<br />   &lt;?php print Honeypot::getHoneypot(); ?&gt;<br />     ...<br /> &lt;/form&gt;</code></p> <h4><br /> At the form submission:</h4> <p><code>if (Honeypot::checkHoneypot()) {<br />   // Probably a spam.<br /> }<br /> else {<br />   // Probably not a spam.<br /> }</code></p> <p> </p> <p>Source : http://stackoverflow.com/questions/36227376/better-honeypot-implementation-form-anti-spam</p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/php" hreflang="en">PHP</a></li> <li><a href="/en/tags/form" hreflang="en">Form</a></li> </ul> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=296&amp;2=comment&amp;3=comment" token="Uv55kBioc5f505te4tnAMNmBM2twTIq1T941A6QEOrY"></drupal-render-placeholder> </section> Thu, 16 Mar 2017 09:14:04 +0000 editor 296 at http://drupal8.ovh Create a View Plugin Without using Database field http://drupal8.ovh/en/tutoriels/293/create-a-view-plugin-without-using-database-field <span class="field field--name-title field--type-string field--label-hidden">Create a View Plugin Without using Database field</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to create a View Plugin Without using Database field , to use with an existence entity (Node, Taxonomy, user ...) ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Fri, 03/03/2017 - 13:53</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h3>Step 1. Create a <a href="/en/tutoriels/3/create-a-simple-module-for-drupal-8">custom module</a> and add the HOOK hook_views_data_alter(&amp;$data). Here module : <strong>mymodule</strong></h3> <h3>Step 2. Alter views data, Example</h3> <p><code>function mymodule_views_data_alter(&amp;$data) {<br />   //Add to <strong>Node</strong> views<br />   $data['node']['<strong>random_value</strong>'] = array(<br />     'field' =&gt; array(<br />       'id' =&gt; '<strong>random_value</strong>',<br />       'title' =&gt; t('Random value'),<br />       'help' =&gt; t('Example of random value plugin.'),<br />     ),<br />   );</code></p> <p><code>  //Add to <strong>Taxonomy</strong> views<br />   $data['taxonomy_term_data']['<strong>random_value</strong>'] = array(<br />     'field' =&gt; array(<br />       'id' =&gt; '<strong>random_value</strong>',<br />       'title' =&gt; t('Random value'),<br />       'help' =&gt; t('Example of random value plugin.'),<br />     ),<br />   );</code></p> <p><code>  //Add to <strong>Users</strong> views<br />   $data['users']['<strong>random_value</strong>'] = array(<br />     'field' =&gt; array(<br />       'id' =&gt; '<strong>random_value</strong>',<br />       'title' =&gt; t('Random value'),<br />       'help' =&gt; t('Example of random value plugin.'),<br />     ),<br />   );</code><br /> }</p> <p> </p> <h3>Step 3. Create view plugin on your custom mudule. (Here the minimum necessary to create a plugin)</h3> <p> </p> <p><strong>Examlple : mymodule/src/Plugin/views/field/RandomValue.php</strong></p> <p>Plugin with minimim settings.</p> <p><code>&lt;?php</code></p> <p><code>namespace Drupal\mymodule\Plugin\views\field;</code></p> <p><code>use Drupal\views\Plugin\views\field\FieldPluginBase;<br /> use Drupal\views\ResultRow;<br /> use Drupal\Core\Form\FormStateInterface;</code></p> <p><code>/**<br />  * Default implementation of the base field plugin,<br />  *<br />  * @ingroup views_field_handlers<br />  *<br />  * @ViewsField("random_value")<br />  */<br /> class RandomValue extends FieldPluginBase {</code></p> <p><em><strong><code>  /**<br />    * This is the most important part of this plugin.<br />    * Empty query methode, Allow to create field without database table field.<br />    */<br />   public function query() {<br />     //Must leave blank<br />   }</code></strong></em></p> <p><code>  /**<br />    * {@inheritdoc}<br />    */<br />   public function render(ResultRow $values) {<br />     //Retun your custom vaule, Here, a random value.<br />     return "Random value is : " . rand(0, 1000);<br />   }</code></p> <p><code>}</code></p> <p> </p> <p> </p> <h2> Complete plugin with few other settings</h2> <p><code>&lt;?php</code></p> <p><code>namespace Drupal\mymodule\Plugin\views\field;</code></p> <p><code>use Drupal\views\Plugin\views\field\FieldPluginBase;<br /> use Drupal\views\ResultRow;<br /> use Drupal\Core\Form\FormStateInterface;</code></p> <p><code>/**<br />  * Default implementation of the base field plugin,<br />  *<br />  * @ingroup views_field_handlers<br />  *<br />  * @ViewsField("random_value")<br />  */<br /> class RandomValue extends FieldPluginBase {</code></p> <p><code>  /**<br />    * This is the most important part of this plugin.<br />    * Empty query methode, Allow to create field without database table field.<br />    */<br />   public function query() {<br />     //Must leave blank<br />   }</code></p> <p><code>  /**<br />    * {@inheritdoc}<br />    */<br />   protected function defineOptions() {<br />     $options = parent::defineOptions();<br />     $options['add_text'] = array('default' =&gt; 0);<br />     return $options;<br />   }</code></p> <p><code>  /**<br />    * {@inheritdoc}<br />    */<br />   public function buildOptionsForm(&amp;$form, FormStateInterface $form_state) {</code></p> <p><code>    $form['add_text'] = array(<br />       '#title' =&gt; t('Add a text'),<br />       '#type' =&gt; 'checkbox',<br />       '#default_value' =&gt; $this-&gt;options['add_text'],<br />     );</code></p> <p><code>    parent::buildOptionsForm($form, $form_state);<br />   }</code></p> <p><code>  /**<br />    * {@inheritdoc}<br />    */<br />   public function submitOptionsForm(&amp;$form, FormStateInterface $form_state) {</code></p> <p><code>    $this-&gt;options['add_text'] = $form_state-&gt;getValue('add_text');</code></p> <p><code>    parent::buildOptionsForm($form, $form_state);<br />   }</code></p> <p><code>  /**<br />    * {@inheritdoc}<br />    */<br />   public function adminSummary() {<br />     return $this-&gt;options['add_text'] ? "With text" : "no text";<br />   }</code></p> <p><code>  /**<br />    * {@inheritdoc}<br />    */<br />   public function render(ResultRow $values) {</code></p> <p><code>    return ($this-&gt;options['add_text'] ? "Random value is : " : "") . rand(0, 1000);<br />   }</code></p> <p><code>}</code></p> <p> </p> <h2>Finally</h2> <p><strong>Now you can add this field to your Node, Taxonomy and Users Views.</strong></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/module" hreflang="en">Module</a></li> <li><a href="/en/tags/views" hreflang="en">Views</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/132/views-custom-tables-hook-views-data" hreflang="en">Views with custom Tables - hook_views_data</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=293&amp;2=comment&amp;3=comment" token="Dzzkd66-orVnFdNmU3CdEN3kQIlweU3fxaMmIC2ukXU"></drupal-render-placeholder> </section> Fri, 03 Mar 2017 12:53:38 +0000 editor 293 at http://drupal8.ovh Override 40x System error pages on Drupal 8 http://drupal8.ovh/en/tutoriels/292/override-40x-system-error-pages-on-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Override 40x System error pages on Drupal 8</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to override 401, 403 and 404 error pages on Drupal 8 ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mon, 02/20/2017 - 16:31</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>You have several mthodes to override 403 / 404 System pages.</p> <h2>Method 1. Set 403 / 404 pages.</h2> <p>You can set the 403 /404 page on "Basic site settings" page<br /> Admin -&gt; Configuration -&gt; System -&gt; Basic site settings<br /> Path /admin/config/system/site-information</p> <h2>Method 2. Override system 40x Pages (/system/404 ...)</h2> <p>Step 1. <a href="/en/tutoriels/3/create-a-simple-module-for-drupal-8">Create a custom module</a> (Here : 'mymodule').</p> <p>Step 2. <a href="/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page">Create a basic page</a> (Here : \Drupal\mymodule\Controller\MyErrorPage:on404).</p> <p>Step 3. Add routing.yml file (Here : mymodule.routing.yml).<br /> Example:<br /><code>mymodule.system.404:<br />   path: '/system/404'<br />   defaults:<br />     _controller: '\Drupal\mymodule\Controller\MyErrorPage:on404'<br />     _title: 'Page not found - 404'<br />   requirements:<br />     _access: 'TRUE'</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page" hreflang="en">Create a simple module for Drupal 8 - Step 2 - Create Simple Page</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=292&amp;2=comment&amp;3=comment" token="rd7Kixai5Y7_CtLTiVvVSzK46mg2k__2a1f2lSxDUEI"></drupal-render-placeholder> </section> Mon, 20 Feb 2017 15:31:32 +0000 editor 292 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/292/override-40x-system-error-pages-on-drupal-8#comments Render a Render array to HTML code. http://drupal8.ovh/en/tutoriels/290/render-a-render-array-to-html-code <span class="field field--name-title field--type-string field--label-hidden">Render a Render array to HTML code.</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to get HTML from drupal 8 renderer array ?<br /> How to convert Render array into HTML output code ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Fri, 02/10/2017 - 21:16</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Example :</p> <p><code>$result =  array(<br />   '#markup' =&gt; 'Hello. This is my First Page',<br /> );<br /> $renderer = \Drupal::service('renderer');<br /> $html = $renderer-&gt;render($result);</code></p> <p> </p> <h3>Render outside of the Render context:</h3> <p><code>$renderer = \Drupal::service('renderer');<br /> $html = $renderer-&gt;renderPlain($result);<br /> #OR<br /> $html = $renderer-&gt;renderRoot($result);</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/module" hreflang="en">Module</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/100/display-contents-using-drupal8-render-api" hreflang="en">Display contents using Drupal8 Render API</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=290&amp;2=comment&amp;3=comment" token="s_rgntqZMesJQZpzsucmqiXzu5rAh9JFWG31guqpjAk"></drupal-render-placeholder> </section> Fri, 10 Feb 2017 20:16:23 +0000 editor 290 at http://drupal8.ovh