Drupal 8 http://drupal8.ovh/en/tags/drupal-8 en 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="ec8dd685"></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="a4c14b10"></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="05f8cffe"></drupal-render-placeholder> </section> Mon, 20 Feb 2017 15:31:32 +0000 editor 292 at http://drupal8.ovh 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="9cfac063"></drupal-render-placeholder> </section> Fri, 10 Feb 2017 20:16:23 +0000 editor 290 at http://drupal8.ovh Insert a view on a custom page or block http://drupal8.ovh/en/tutoriels/289/insert-a-view-on-a-custom-page-or-block <span class="field field--name-title field--type-string field--label-hidden">Insert a view on a custom page or block</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 add and render a views page or block in a custom page or block 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">Fri, 02/10/2017 - 11:55</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>This tutorial show toy how to insert an existing views into a <a href="/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page">custom page</a> or <a href="/en/tutoriels/107/create-a-simple-block-programmatically">custom block</a>.</p> <p> </p> <h3>Render a views block.</h3> <p><strong>Example : Show Who's online block</strong></p> <p><code>$view = Views::getView('who_s_online');<br /> if (is_object($view)) {<br />   $view-&gt;setDisplay('who_s_online_block');<br />   $view-&gt;preExecute();<br />   $view-&gt;execute();</code><br /><code>  //$content = $view-&gt;buildRenderable('who_s_online_block');<br />   //$content = $view-&gt;buildRenderable('block', $args);</code><br /><code>  $content = $view-&gt;buildRenderable();<br />   $output[] = $content;<br /> }</code></p> <h3> Render a views page with selected exposed filter.</h3> <p><strong>Example : Show People page with user name's contains 'admin' and has administrator role</strong></p> <p><code>$view = Views::getView('user_admin_people');<br /> if (is_object($view)) {<br />   $view-&gt;setExposedInput([<br />     'role' =&gt; 'administrator',<br />     'user' =&gt; 'admin',<br />   ]);<br />   $view-&gt;setDisplay('page_1');<br />   $view-&gt;preExecute();<br />   $view-&gt;execute();</code><br /><code>  //$content = $view-&gt;buildRenderable('page_1');</code><br /><code>  $content = $view-&gt;buildRenderable();<br />   $output[] = $content;<br /> }</code></p> <p> </p> <p><strong>Few other configurations:</strong></p> <p><code>$view-&gt;setArguments([]);<br /> $view-&gt;setItemsPerPage(10);<br /> $view-&gt;setOffset(5);<br /> $view-&gt;setTitle("A new title");</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/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/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> <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=289&amp;2=comment&amp;3=comment" token="734b86c1"></drupal-render-placeholder> </section> Fri, 10 Feb 2017 10:55:34 +0000 editor 289 at http://drupal8.ovh Load entity dynamicly by entity type on Drupal 8 http://drupal8.ovh/en/tutoriels/288/load-entity-dynamicly-by-entity-type-on-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Load entity dynamicly by entity type 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 load drupal 8 entity dynamicly by entity type and entity id ?</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 - 11:44</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h3>Using entity_load function.</h3> <p>Example:<br /><code>$entity = entity_load($entity_type, $id);</code></p> <h3>Using Entity Manager</h3> <p>Example:<br /><code>$entity = \Drupal::entityTypeManager()-&gt;getStorage($entity_type)-&gt;load($id);</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/entity" hreflang="en">Entity</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=288&amp;2=comment&amp;3=comment" token="d272022f"></drupal-render-placeholder> </section> Fri, 10 Feb 2017 10:44:12 +0000 editor 288 at http://drupal8.ovh Change the state of an element based on another element (field) http://drupal8.ovh/en/tutoriels/286/change-state-of-an-element-based-on-another-element-field <span class="field field--name-title field--type-string field--label-hidden">Change the state of an element based on another element (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 change the visibility of an afield element based on another field element ?</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, 01/31/2017 - 17:18</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>The field API adds JavaScript to change the state of an element based on another element.</p> <h3>Example 1.</h3> <p><code>$form['testfield'] = array(<br />   '#type' =&gt; 'select',<br />   '#title' =&gt; $this-&gt;t('Select'),<br />   '#options' =&gt; array('a' =&gt; 'a', 'b' =&gt; 'b', 'c' =&gt; 'c'),<br /> );</code></p> <p><code>$form['display'] = array(<br />   '#title' =&gt; t('Just display'),<br />   '#type' =&gt; 'textfield',<br />   '#states' =&gt; array(<br />     'visible' =&gt; array(<br />       ':input[id="edit-settings-testfield"]' =&gt; array('value' =&gt; 'b'),<br />     ),<br />   ),<br /> );</code></p> <h3> Example 2.</h3> <p><code>$form['testfield'] = array(<br />   '#type' =&gt; 'checkbox',<br />   '#title' =&gt; $this-&gt;t('Checkbox'),<br /> );<br /> //Example 2 - 1<br /> $form['display1'] = array(<br />   '#title' =&gt; t('1 - Just display : visible if checked'),<br />   '#type' =&gt; 'textfield',<br />   '#states' =&gt; array(<br />     'visible' =&gt; array(<br />       ':input[name$="[testfield]"]' =&gt; array('checked' =&gt; TRUE),<br />     ),<br />   ),<br /> );<br /> //Example 2 - 2<br /> $form['display2'] = array(<br />   '#title' =&gt; t('2 - Just display : visible if checked'),<br />   '#type' =&gt; 'textfield',<br />   '#states' =&gt; array(<br />     'visible' =&gt; array(<br />       ':input[name$="settings[testfield]"]' =&gt; array('checked' =&gt; TRUE),<br />     ),<br />   ),<br /> );<br /> //Example 2 - 3<br /> $form['display3'] = array(<br />   '#type' =&gt; 'container',<br />   '#states' =&gt; array(<br />     'visible' =&gt; array(<br />       ':input[id="edit-settings-testfield"]' =&gt; array('checked' =&gt; TRUE),<br />     ),<br />   ),<br /> );<br /> $form['display3']['a_markup'] = array(<br />   '#type' =&gt; 'markup',<br />   '#markup' =&gt; "3 - Just a text, visible if checked",<br /> );<br /> //Example 2 - 4<br /> $form['display4'] = array(<br />   '#title' =&gt; t('4 - invisible if checked'),<br />   '#type' =&gt; 'textfield',<br />   '#states' =&gt; array(<br />     'invisible' =&gt; array(<br />       ':input[id="edit-settings-testfield"]' =&gt; array('checked' =&gt; TRUE),<br />     ),<br />   ),<br /> );</code></p> <h3>Example 3 : Usage in a FieldFormatter (Drupal\Core\Field\FormatterBase ...)</h3> <p><code>  $elements['selectable'] = [<br />     '#title' =&gt; t('Selectable'),<br />     '#type' =&gt; 'checkbox',<br />     '#default_value' =&gt; $this-&gt;getSetting('selectable'),<br />   ];<br />   $elements['style'] = array(<br />     '#title' =&gt; t('Apply CSS'),<br />     '#type' =&gt; 'checkbox',<br />     '#default_value' =&gt; $this-&gt;getSetting('style'),<br />     '#states' =&gt; array(<br />       'visible' =&gt; array(<br />         ':input[name$="[settings_edit_form][settings][selectable]"]' =&gt; array('checked' =&gt; TRUE),<br />       ),<br />     ),<br />   );</code></p> <p> </p> <h3>Example 4 : Multiple posibilities (OR)</h3> <h3><code>'visible' =&gt; array(<br />   array(<br />     ':input[name="name"]' =&gt; array('filled' =&gt; TRUE),<br />   ),<br />   array(<br />     ':select[name="method"]' =&gt; array('value' =&gt; 'email'),<br />   ),<br /> ),<br /> ),</code><br />  <br /> Example 5 : Multiple conditions (AND)</h3> <p><code>'visible' =&gt; array(<br />   array(<br />     ':input[name="name"]' =&gt; array('filled' =&gt; TRUE),<br />     ':select[name="method"]' =&gt; array('value' =&gt; 'email'),<br />   ),<br /> ),<br /> ),</code></p> <p> </p> <p>Help : https://api.drupal.org/api/drupal/core%21includes%21common.inc/function/drupal_process_states/8.2.x<br /> More examples : https://www.lullabot.com/articles/form-api-states</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/field" hreflang="en">Field</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/139/add-remove-fields-dynamically-using-form-api-and-ajax" hreflang="en">Add Remove Fields Dynamically using Form API and Ajax</a></div> <div class="field__item"><a href="/en/tutoriels/210/preprocess-a-field-change-theming-of-a-field" hreflang="en">Preprocess a Field, Change Theming of a field</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=286&amp;2=comment&amp;3=comment" token="ab9e207e"></drupal-render-placeholder> </section> Tue, 31 Jan 2017 16:18:05 +0000 editor 286 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/286/change-state-of-an-element-based-on-another-element-field#comments Use multiple databases on drupal 8 http://drupal8.ovh/en/tutoriels/285/use-multiple-databases-on-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Use multiple databases 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 use multiple databases to move some tables from main database.</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, 01/18/2017 - 17:23</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Move some tables into another database.</h2> <p>Example:</p> <p><code>$databases['default']['default'] = array(<br />   'database' =&gt; 'drupal8_default',<br />   'username' =&gt; 'root',<br />   'password' =&gt; 'THEPASSWORD',<br />   'host' =&gt; 'localhost',<br />   'port' =&gt; '3306',<br />   'namespace' =&gt; 'Drupal\\Core\\Database\\Driver\\mysql',<br />   'driver' =&gt; 'mysql',</code></p> <p><code>  //Add prefix to separate databases<br />   'prefix' =&gt; array(<br />     //Use a shared database for the users (not recomended, Just for example)<br />     'users' =&gt; 'drupal8_shared.',</code></p> <p><code>    //Move cache into another database<br />     'cache_bootstrap' =&gt; 'drupal8_cache.',<br />     'cache_config' =&gt; 'drupal8_cache.',<br />     'cache_container' =&gt; 'drupal8_cache.',<br />     'cache_data' =&gt; 'drupal8_cache.',<br />     'cache_default' =&gt; 'drupal8_cache.',<br />     'cache_discovery' =&gt; 'drupal8_cache.',<br />     'cache_dynamic_page_cache' =&gt; 'drupal8_cache.',<br />     'cache_entity' =&gt; 'drupal8_cache.',<br />     'cache_menu' =&gt; 'drupal8_cache.',<br />     'cache_render' =&gt; 'drupal8_cache.',<br />     'cache_toolbar' =&gt; 'drupal8_cache.',<br />   ),<br /> );</code></p> <p><code>//Configure shared database<br /> $databases['shared']['default'] = array(<br />   'database' =&gt; 'drupal8_shared',<br />   'username' =&gt; 'root',<br />   'password' =&gt; 'THEPASSWORD',<br />   'host' =&gt; 'localhost',<br />   'port' =&gt; '3306',<br />   'namespace' =&gt; 'Drupal\\Core\\Database\\Driver\\mysql',<br />   'driver' =&gt; 'mysql',<br /> );</code></p> <p><code>//Configure cache database<br /> $databases['cache']['default'] = array(<br />   'database' =&gt; 'drupal8_cache',<br />   'username' =&gt; 'root',<br />   'password' =&gt; 'THEPASSWORD',<br />   'host' =&gt; 'localhost',<br />   'port' =&gt; '3306',<br />   'namespace' =&gt; 'Drupal\\Core\\Database\\Driver\\mysql',<br />   'driver' =&gt; 'mysql',<br /> );</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/database" hreflang="en">Database</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/175/use-multiple-databases-drupal-8" hreflang="en">Use multiple / external databases in 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=285&amp;2=comment&amp;3=comment" token="93a5ab0d"></drupal-render-placeholder> </section> Wed, 18 Jan 2017 16:23:02 +0000 editor 285 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/285/use-multiple-databases-on-drupal-8#comments Create an advances Search system for Drupal 8 with Search API. http://drupal8.ovh/en/tutoriels/284/create-a-advances-search-system-for-drupal-8-search-api <span class="field field--name-title field--type-string field--label-hidden">Create an advances Search system for Drupal 8 with Search API.</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 advances search server like Solr, Elasticsearch, Database ... With a Drupal 8 site ?</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, 01/18/2017 - 12:03</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Search API and Database Search server.</h2> <p>1. Uninstall drupal basic Search<br /> 2. Install search_api and search_api_db<br />   NOTE : You can also use another database configures on settings.php<br /> 3. Add a Search Server (/admin/config/search/search-api/add-server).<br />   - Use "Database" as cackend server.<br /> 3. Add a Search Index (/admin/config/search/search-api/add-index).<br />   - Select Enteties (Data source) to index with this index and a search server.<br />   - Save and Add fields to Index. (Example : Title, Body file for "Contents").<br />   - Optionaly you can configure "Processors" as Filters, Data traitements ...<br /> 4. Enable the newly created "Index" then run indexing or queue indexing.<br /> 5. Create a view using your Index, and add field "Search: Fulltext search" as Exposed criteris filter.</p> <p>Once indexing is done, visit the view's page to use the search system.</p> <p> </p> <p><strong>Tips </strong>: For a quick start, just install the "Database Search Defaults" (), and configure your "Search Server", "Index" and "Search Pages" on.<br />  - Search API :        /admin/config/search/search-api<br />  - Search Views :    /admin/structure/views (Defaut : /admin/structure/views/view/search_content )</p> <p><em>NOTE : To create a server like Solr, Elasticsearch you need to install search server and the drupal module for those servers.</em></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/search" hreflang="en">Search</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/273/solr-configuration-for-drupal" hreflang="en">Solr Configuration for Drupal</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=284&amp;2=comment&amp;3=comment" token="329c2fe3"></drupal-render-placeholder> </section> Wed, 18 Jan 2017 11:03:57 +0000 editor 284 at http://drupal8.ovh Create a field in a node entity programmatically on Drupal 8. http://drupal8.ovh/en/tutoriels/283/create-a-field-a-node-entity-programmatically-on-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Create a field in a node entity programmatically 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 create a custom field in a node entity programmatically, using field API 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">Fri, 01/13/2017 - 11:37</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Example.<br /> Step 1 : Create field storage.<br /><code>\Drupal\field\Entity\FieldStorageConfig::create(array(<br />   'field_name' =&gt; 'field_text',<br />   'entity_type' =&gt; 'node',<br />   'type' =&gt; 'text',<br />   'cardinality' =&gt; -1,<br /> ))-&gt;save();</code></p> <p>Step 2 : Attach an instance of the field to the page content type.<br /><code>\Drupal\field\Entity\FieldConfig::create([<br />   'field_name' =&gt; 'field_text',<br />   'entity_type' =&gt; 'node',<br />   'bundle' =&gt; 'page',<br />   'label' =&gt; 'A Text field',<br /> ])-&gt;save();</code></p> <p>Step 3 : Set From Display<br /><code>entity_get_form_display('node', 'page', 'default')<br />   -&gt;setComponent('field_text', array(<br />     'type' =&gt; 'text_textfield',<br />   ))<br />   -&gt;save();</code></p> <p>Step 4 : Set Display<br /><code>entity_get_display('node', 'page', 'default')<br />   -&gt;setComponent('field_text', array(<br />     'type' =&gt; 'text_default',<br />   ))<br />   -&gt;save();</code></p> <p>Tips</p> <p>Load the field:<br /><code>$field_config = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', 'field_text');</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/field" hreflang="en">Field</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/263/custom-content-entity-field-types-of-drupal-8" hreflang="en">Custom Content Entity Field types of drupal 8</a></div> <div class="field__item"><a href="/en/tutoriels/58/get-field-value-of-a-node-entity" hreflang="en">Get field value of a Node / Entity</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-121"></a> <article role="article" data-comment-user-id="0" about="/en/comment/121" typeof="schema:Comment" class="comment js-comment by-anonymous clearfix"> <span class="hidden" data-comment-timestamp="1485762761"></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="">Sanal M (not verified)</span></span> </p> <p class="comment__time">Mon, 01/30/2017 - 08:52 <span property="schema:dateCreated" content="2017-01-30T07:52:41+00:00" class="rdf-meta hidden"></span> </p> <p class="comment__permalink"><a href="/en/comment/121#comment-121" hreflang="en">Permalink</a></p> </footer> <div class="comment__content"> <h3 property="schema:name" datatype=""><a href="/en/comment/121#comment-121" class="permalink" rel="bookmark" hreflang="en">saving the code</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>But where i have save this code on my drupal.</p> </div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=121&amp;1=default&amp;2=en&amp;3=" token="260dffd9"></drupal-render-placeholder> </div> </article> <div class="indented"><a id="comment-124"></a> <article role="article" data-comment-user-id="2" about="/en/comment/124" typeof="schema:Comment" class="comment js-comment by-node-author clearfix"> <span class="hidden" data-comment-timestamp="1485865787"></span> <footer class="comment__meta"> <article typeof="schema:Person" about="/index.php/en/users/editor" class="profile"> </article> <p class="comment__author"><span rel="schema:author"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> </p> <p class="comment__time">Tue, 01/31/2017 - 13:29 <span property="schema:dateCreated" content="2017-01-31T12:29:47+00:00" class="rdf-meta hidden"></span> </p> <p class="comment__permalink"><a href="/en/comment/124#comment-124" hreflang="en">Permalink</a></p> <p class="visually-hidden">In reply to <a href="/en/comment/121#comment-121" class="permalink" rel="bookmark" hreflang="en">saving the code</a> by <span lang="" typeof="schema:Person" property="schema:name" datatype="">Sanal M (not verified)</span></p> </footer> <div class="comment__content"> <h3 property="schema:name" datatype=""><a href="/en/comment/124#comment-124" class="permalink" rel="bookmark" hreflang="en">Create a new Module</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>For that, you must create a new module. Follow this tutorial: <a href="/en/tutoriels/3/create-a-simple-module-for-drupal-8">/en/tutoriels/3/create-a-simple-module-for-drupal-8</a> and <a href="/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page">/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page</a></p> </div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=124&amp;1=default&amp;2=en&amp;3=" token="66507c66"></drupal-render-placeholder> </div> </article> </div> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=283&amp;2=comment&amp;3=comment" token="e3d2bdeb"></drupal-render-placeholder> </section> Fri, 13 Jan 2017 10:37:42 +0000 editor 283 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/283/create-a-field-a-node-entity-programmatically-on-drupal-8#comments