Code http://drupal8.ovh/en/tags/code en 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> </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> <p>Help : https://api.drupal.org/api/drupal/core%21includes%21common.inc/function/drupal_process_states/8.2.x</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 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 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="/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 CSS Styling tips for Print http://drupal8.ovh/en/tutoriels/282/css-styling-tips-for-print <span class="field field--name-title field--type-string field--label-hidden">CSS Styling tips for Print</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 css for print 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">Thu, 01/12/2017 - 15:56</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Add CSS for Print</h2> <h3>Method 1 : Add a specific styling sheet for Print.</h3> <p>On your theme (THEME.info.yml for D8 and THEME.info for D7), Add css file as [print].<br /> Example<br /><code>stylesheets[print][] = css/print.css</code></p> <p> </p> <h3>Method 1. In Same style sheet.</h3> <p>Wrap codes for print using @media query.<br /> Example:<br /><code>@media print<br /> {<br />   /*Codes For Print*/<br /> }</code></p> <h2>Tips:</h2> <h3>Remove the URL and SRC from print css (Hide address)</h3> <p><code>  a[href]:after { content: none !important; }<br />   img[src]:after { content: none !important; }</code></p> <h3>Add Print Button (Or image)</h3> <p>Example:<br /><code>&lt;img src="print.png" alt="Print" onclick="window.print()"&gt;<br /> &lt;button onclick="window.print()"&gt;Print&lt;/button&gt;</code></p> <h3> </h3> <h3>Remove / Replace the html "Select" (selectbox's) dropdown arrow</h3> <p><code>  select {<br />     //Remove default arrow<br />     appearance: none;<br />     -moz-appearance: none;<br />     -webkit-appearance: none;<br />     &amp;::-ms-expand {<br />       display: none;<br />     }<br />     //Replace with an image<br />     background-repeat: no-repeat;<br />     background-image: url("/path_to/selecebox_arrow.png");<br />     background-position: right;<br />   }</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/264/basic-css-styling-tips" hreflang="en">Basic CSS Styling tips</a></div> <div class="field__item"><a href="/en/tutoriels/22/add-css-to-a-module" hreflang="en">Add CSS stylesheets to a module</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=282&amp;2=comment&amp;3=comment" token="42eb3905"></drupal-render-placeholder> </section> Thu, 12 Jan 2017 14:56:34 +0000 editor 282 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/282/css-styling-tips-for-print#comments Make Some operations Programmatically - Hook Update Deploy Tools http://drupal8.ovh/en/tutoriels/281/make-some-operations-programmatically-hook-update-deploy-tools <span class="field field--name-title field--type-string field--label-hidden">Make Some operations Programmatically - Hook Update Deploy Tools</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 make deploy operations programmatically ?</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/11/2017 - 11:33</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p><strong>Hook Update Deploy Tools</strong> is a great contrib module help to make most of site deploy operations programmatically.<br /> Link : https://www.drupal.org/project/hook_update_deploy_tools</p> <p>CAUTION : Current drupal 8 version is unstable (8.x-1.0-unstable1, 2016-Feb-07 Version),  but 8.x-1.x-dev version is usable.</p> <h3>Example: Delete a field instance.<br /> Using contrib module hook_update_deploy_tools</h3> <p>//Drupal 8:<br /> $message = \HookUpdateDeployTools\Fields::deleteInstance('taxonomy_term', 'field_name', 'tags');</p> <p>//Drupal 7:  <br /> $message = HookUpdateDeployTools\Fields::deleteInstance('taxonomy_term', 'field_name', 'tags');</p> <p> </p> <p>Without Hook Update Deploy Tools<br /> //Drupal 8:</p> <p>//Drupal 7:<br /> $instance = field_info_instance('node', 'field_name', 'page');//For node<br /> $instance = field_info_instance('taxonomy_term', 'field_name', 'tags');//For taxonomy term<br /> if ($instance) {<br />   field_delete_instance($instance);<br /> }</p> <p>//D8 (Not tested)<br /> $field_info = \Drupal\field\Entity\FieldConfig::loadByName('node', 'page', 'field_name');<br /> $field_info-&gt;delete();</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/drupal-7" hreflang="en">Drupal 7</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/223/change-drupal-8-text-field-maximum-length" hreflang="en">Change Drupal 8 text field maximum length</a></div> <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> <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=281&amp;2=comment&amp;3=comment" token="7ad0b276"></drupal-render-placeholder> </section> Wed, 11 Jan 2017 10:33:42 +0000 editor 281 at http://drupal8.ovh Get Entity data and metadata http://drupal8.ovh/en/tutoriels/280/get-entity-deta-and-metadata <span class="field field--name-title field--type-string field--label-hidden">Get Entity data and metadata</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 Entity type data and metadata</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/10/2017 - 13:11</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Get entity types list :</p> <p><span>$itmes_list= \Drupal::</span><em>entityTypeManager</em><span>()-&gt;getDefinitions();</span></p> <p><span>Get Entity type (Content / Config)</span></p> <p><span>$itmes_list['node']-&gt;getGroup();<br /> $itmes_list['node_type']-&gt;getGroup();</span></p> <p><span>Get Label / Name</span></p> <p><span>$itmes_list['node']-&gt;getLabel();</span></p> <p> </p> <p>Load entity (dynamic type) from database</p> <p>\Drupal::entityTypeManager()-&gt;getStorage($entity_type)-&gt;load($id);<br /> See : https://api.drupal.org/api/drupal/core%21includes%21entity.inc/function/entity_load/8.3.x</p> <h2>Load a entity type using entityTypeManager</h2> <p>Example :<br /><code>$nodeEntity = \Drupal::entityTypeManager()-&gt;getDefinition('node');</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> <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/149/find-entity-using-drupal-entityquery" hreflang="en">Find Entity using Drupal EntityQuery</a></div> <div class="field__item"><a href="/en/tutoriels/149/find-entity-using-drupal-entityquery" hreflang="en">Find Entity using Drupal EntityQuery</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=280&amp;2=comment&amp;3=comment" token="dbe93698"></drupal-render-placeholder> </section> Tue, 10 Jan 2017 12:11:53 +0000 editor 280 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/280/get-entity-deta-and-metadata#comments