Drupal 8 - Drupal 8 - News - Help - Tutorial - And more... http://drupal8.ovh/en/rss.xml en Test a webpage for Retina display http://drupal8.ovh/en/tutoriels/325/test-a-webpage-for-retina-display <span class="field field--name-title field--type-string field--label-hidden">Test a webpage for Retina display</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 test a webpage meant for Retina display ?</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, 06/19/2017 - 10:56</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>To test retina display, just adjust the screen pixel ratio.</p> <h2>On Firefox</h2> <p>1. Go to the  Firefox advance configuration. (Type <code><strong>about:config</strong></code> on address bar)<br /> 2. Find <code><strong>layout.css.devPixelsPerPx</strong></code><br /> 3. Set ratio to 1.5, 2.0 .... (1 is normal, -1.0 is default value)<br /> Firefox</p> <p>Default : layout.css.devPixelsPerPx = -1.0</p> <h2> On Chrome</h2> <p>1. F12 to open developer tools<br /> 2. Enable mobile emulation (Ctrl + Shift + M or Click on Mobile/Tablet icon)<br /> 3. At the top right of the page, click on menu icon (Three small points<code> ⋮ </code>) Then "Add device pixel ration".<br /> 4. Now you can see a new dropdown menu "DPR:....", Set Device type to Responsive to change manually</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/tests" hreflang="en">Tests</a></li> <li><a href="/en/tags/css" hreflang="en">CSS</a></li> <li><a href="/en/tags/theming" hreflang="en">Theming</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/282/css-styling-tips-for-print" hreflang="en">CSS Styling tips for Print</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 class="field__item"><a href="/en/tutoriels/294/display-a-web-page-as-print-media-print-emulate-for-development-and-test" hreflang="en">Display a web page as print. Media Print Emulate For development and test</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=325&amp;2=comment&amp;3=comment" token="u9aWd06KUzZU8LY7kZ_riJs7G8ZvpfAPTbWsjZmG2Oc"></drupal-render-placeholder> </section> Mon, 19 Jun 2017 08:56:15 +0000 editor 325 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/325/test-a-webpage-for-retina-display#comments Linux shell output - Handle StdOut http://drupal8.ovh/en/tutoriels/324/linux-shell-output-handle-stdout <span class="field field--name-title field--type-string field--label-hidden">Linux shell output - Handle StdOut</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 manage linux shell stdout ?<br /> How to save linux shell output to a file ?<br /> How to ptint linux shell output (stdout) to the terminal and save to a file ?<br /> How redirect a copy of stdout to log file from within bash script itself ?</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, 06/16/2017 - 11:03</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>The linux shell/bash terminal default behaviour is print output to the terminal (StdOut : Standard output). But sometimes we need to change this behaviour like save toi a file, display noting ... Here some exaple of user case.</p> <h2>Print output to a file (log.txt)</h2> <p>Example : Save ping outout to a file.<br /><code>ping -c 4 drupal.org &gt; log.txt</code></p> <p>So this command will create a file log.txt contains:<br /><code>PING drupal.org (151.101.1.175) 56(84) bytes of data.<br /> 64 bytes from 151.101.1.175: icmp_seq=1 ttl=60 time=28.8 ms<br /> 64 bytes from 151.101.1.175: icmp_seq=2 ttl=60 time=77.0 ms<br /> 64 bytes from 151.101.1.175: icmp_seq=3 ttl=60 time=48.3 ms<br /> 64 bytes from 151.101.1.175: icmp_seq=4 ttl=60 time=16.5 ms<br /> --- drupal.org ping statistics ---<br /> 4 packets transmitted, 4 received, 0% packet loss, time 3003ms<br /> rtt min/avg/max/mdev = 16.521/42.689/77.006/22.838 ms</code></p> <h2> Print shell script output to a file (From terminal)</h2> <p>Script (test.sh):<br /><code>#!/bin/bash<br /> date; # This will print the date</code></p> <p>Command<br /><code>./test.sh &gt; log.txt</code></p> <p>This will display nothis on terminal, but print the date in to the file log.txt, like:<br /><code>Fri Jun 16 11:14:27 CEST 2017</code></p> <h2> Redirect all outputs to a file</h2> <p>To print all outputs of the terminal in to a file, just type exec &gt; THE_FILE_NAME<br /> Example:<br /><code>exec &gt; log.txt<br /> echo "Just test"<br /> date<br /> ... ...</code></p> <h2> Not print the result, just execute the command</h2> <p>Example: run script but print to nothing.<br /><code># Out to nothing<br /> ./test.sh &gt; /dev/null</code></p> <p> </p> <h2>Print a command output to a file and print to the terminal (stdout)</h2> <p>Example:<br /><code>[COMMAND]| tee [OUTPUT_FILE]<br /> date |&amp; tee log.txt</code></p> <h2> Print a script output to a file and print to the terminal, from terminal</h2> <p>Examples:<br /><code>test.sh 2&gt;&amp;1 | tee log.txt<br /> # Or<br /> bash test.sh 2&gt;&amp;1 | tee log.txt</code></p> <h2>Print a script output to a file and print to the terminal, from the script.</h2> <p>Script :<br /><code>#!/bin/bash<br /> #<br /> # Test script<br /><strong>exec &gt;  &gt;(tee -ia log.txt)</strong><br /> # OR<br /> # exec 2&gt; &gt;(tee -ia log.txt &gt;&amp;2)<br /> #<br /> # commands to run.<br /> date;<br /> ping -c 4 drupal.org;</code></p> <p>Command :<br /><code>./test.sh</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/linux" hreflang="en">Linux</a></li> <li><a href="/en/tags/shell" hreflang="en">Shell</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=324&amp;2=comment&amp;3=comment" token="QK_L2fZ7hjJxQ8BRELHskO1ggAyP_oMRGgraPwb_pyA"></drupal-render-placeholder> </section> Fri, 16 Jun 2017 09:03:32 +0000 editor 324 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/324/linux-shell-output-handle-stdout#comments Create a custom DRUSH command on drupal 8 http://drupal8.ovh/en/tutoriels/323/create-a-custom-drush-command-on-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Create a custom DRUSH command 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 drush command for 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, 06/13/2017 - 12:28</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>To create a custom drush command, you must create a custom module <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="b1e18681-bcf2-4efc-bf00-d4f5d9946bbe" href="/en/tutoriels/3/create-a-simple-module-for-drupal-8" rel="nofollow">like this</a> (Here, mymodule).</p> <p>Then add a file : YOUR_MODULE.drush.inc (Here <strong>mymodule.drush.inc</strong>)</p> <p>Example :</p> <p><code>&lt;?php<br /> /**<br />  * Mymodule is a Test module, This is drush command example<br />  * mymodule<br />  */<br /> /**<br />  * Implements hook_drush_command().<br />  */<br /> function mymodule_drush_command() {<br />   $commands = [];<br />   $commands['my-cmd'] = [<br />     'description' =&gt; 'A test command my-cmd.',<br />   ];<br />   $commands['mycommand'] = [<br />     'description' =&gt; 'This is my example command.',<br />     'aliases' =&gt; ['mec'],<br />     'arguments' =&gt; [<br />       'arg1' =&gt; 'My custom argument 1.',<br />       'arg2' =&gt; 'My custom argument 2.',<br />     ],<br />     'options' =&gt; [<br />       'opt1' =&gt; 'My custom option.',<br />     ],<br />     'examples' =&gt; [<br />       'drush mec' =&gt; 'Print my example command.',<br />       'drush mec myargument' =&gt; 'Print my example command with an argument "myargument".',<br />       'drush mec myargument --opt1=myoption' =&gt; 'Print my example command with an argument "myargument" and an option "myoption".',<br />     ],<br />   ];<br />   return $commands;<br /> }<br /> /**<br />  * Call back function drush_custom_drush_command_say_hello()<br />  * The call back function name in the  following format<br />  *   drush_{module_name}_{item_id_for_command}()<br />  */<br /> function drush_mymodule_my_cmd() {<br />   // Your codes<br />   return "My Drush command 'my-cmd' is OK";<br /> }<br /> /**<br />  * Test command 2 : mycommand or mec<br />  */<br /> function drush_mymodule_mycommand($arg = '?') {<br />   // Your codes<br />   $opt1 = drush_get_option('opt1', 'N/A');<br />   drush_print("Argument : $arg, Option : $opt1");<br />   return "My Drush command 'mycommand' is OK";<br /> }</code></p> <p><strong>Note : The procedure is same as on Drupal 7</strong></p> <h2>Tips</h2> <p>To get arguments, you can also use:<br /><code><strong><span>$args </span><span>= </span><em>func_get_args</em><span>();</span></strong></code></p> <p> </p> </div> <div class="field field--name-field-file field--type-file field--label-inline"> <div class="field__label">File</div> <div class="field__item"> <span class="file file--mime-application-zip file--package-x-generic"> <a href="http://drupal8.ovh/sites/drupal/files/2017-06/mymodule.zip" type="application/zip; length=2741">mymodule.zip</a></span> </div> </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/drush" hreflang="en">Drush</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/235/execute-a-drush-command-programmatically" hreflang="en">Execute a drush command programmatically</a></div> <div class="field__item"><a href="/en/tutoriels/98/useful-drush-commands" hreflang="en">Useful Drush commands</a></div> <div class="field__item"><a href="/en/tutoriels/138/install-and-update-drush-on-linux" hreflang="en">Install and Update Drush on Linux</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=323&amp;2=comment&amp;3=comment" token="3gs1bfrFfs-DBDKxYwHn4fTDSiaaAQA3szo7OM5Ywg0"></drupal-render-placeholder> </section> Tue, 13 Jun 2017 10:28:24 +0000 editor 323 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/323/create-a-custom-drush-command-on-drupal-8#comments Alter table structure on Drupal 8 http://drupal8.ovh/en/tutoriels/322/alter-table-structure-on-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Alter table structure 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 update or alter a table structure of drupal 8 , to add fields, remove fields, update fields, add tables, remove tables ... ... ?</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, 06/13/2017 - 11:59</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>To update a table you must create a custom module. <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="b1e18681-bcf2-4efc-bf00-d4f5d9946bbe" href="/en/tutoriels/3/create-a-simple-module-for-drupal-8" rel="nofollow">Example </a>, Here we use mymodule.</p> <p> Note : Initially you must create a database table. Example on<strong> hook_schema()</strong> of <strong>mymodule.install</strong> :<br /><code>/**<br />  * Schema.<br />  */<br /> function mymodule_schema() {<br />   $schema['mymodule_table1'] = [<br />     'description' =&gt; 'My test table',<br />     'fields' =&gt; [<br />       'id' =&gt; [<br />         'type' =&gt; 'serial',<br />         'not null' =&gt; TRUE,<br />       ],<br />       'test_field_1' =&gt; [<br />         'type' =&gt; 'int',<br />       ],<br />     ],<br />     'primary key' =&gt; ['id'],<br />   ];<br />   return $schema;<br /> }</code></p> <h2> Update a table from module update hook</h2> <p>Hook : hook_update_N().<br /><strong>You can update tables from anyware of your module, but it is strongly recomended to use this hook.</strong></p> <p><code>use Drupal\Core\Database\Database;<br /> /**<br />  * Update Table mymodule_table1, add a field and remove a field.<br />  */<br /> mymodule_update_8001(){<br />   $ret = [];<br />   $con = Database::getConnection();<br />   // Remove 'test_field_1' from mymodule_table1 DB.<br />   $ret[] = $con-&gt;schema()-&gt;dropField('mymodule_table1', 'test_field_1');<br />   // Add 'test_field_2' to mymodule_table1 DB.<br />   $spec = [<br />     'type' =&gt; 'text',<br />     'size' =&gt; 'tiny',<br />   ];<br />   $ret[] = $con-&gt;schema()-&gt;addField('mymodule_table1', 'test_field_2', $spec);<br />   return $ret;<br /> }</code></p> <h2> Create a new table</h2> <p><code>$spec = array(<br /> 'description' =&gt; 'My description',<br /> 'fields' =&gt; array(<br />   'myfield1' =&gt; array(<br />     'description' =&gt; 'Myfield1 description.',<br />     'type' =&gt; 'varchar',<br />     'length' =&gt; 255,<br />     'not null' =&gt; TRUE,<br />     'default' =&gt; '',<br />   ),<br />   'myfield2' =&gt; array(<br />     'description' =&gt; 'Myfield2 description',<br />     'type' =&gt; 'text',<br />     'not null' =&gt; TRUE,<br />   ),<br /> ),<br /> 'primary key' =&gt; array('myfield1'),<br /> );<br /> $schema = Database::getConnection()-&gt;schema();<br /> $schema-&gt;createTable('mytable2', $spec);</code></p> <h2> Adding primary keys or indexes</h2> <p><code>$spec = array('myfield1');<br /> $schema = Database::getConnection()-&gt;schema();<br /> // A normal index.<br /> $schema-&gt;addIndex('mytable', $spec);<br /> // A primary key.<br /> $schema-&gt;addPrimaryKey('mytable', $spec);<br /> // A unique key.<br /> $schema-&gt;addUniqueKey('mytable', $spec);</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> <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/159/database-basic-examples" hreflang="en">Database Basic Examples</a></div> <div class="field__item"><a href="/en/tutoriels/72/drupal-mysql-database-data-types" hreflang="en">Drupal Mysql / Database Data types</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=322&amp;2=comment&amp;3=comment" token="12VZ40RyB_xe3AiN8dDlzdPCpvMh65VNJAe1Mjlc3nQ"></drupal-render-placeholder> </section> Tue, 13 Jun 2017 09:59:09 +0000 editor 322 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/322/alter-table-structure-on-drupal-8#comments PHP : Get Class Name http://drupal8.ovh/en/tutoriels/321/php-get-class-name <span class="field field--name-title field--type-string field--label-hidden">PHP : Get Class Name</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 the class name on php ?</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">Sat, 06/10/2017 - 17:14</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>The structure of the examples are like:<br /><code>namespace Drupal\mymodule\Tools;<br /> class MyTools extends MyToolsBase {<br /> ...<br /> }<br /> ...<br /> class MyToolsBase {<br />   public function showClass() {<br />     $class_name = ....<br />     echo $class_name;<br />   }<br /> }</code></p> <h2> PHP Get the class name of a object;</h2> <p>Methode 1.<br /> This will retuen the full clas name including namespace.</p> <p><code>$class_name = (new \ReflectionClass($theObject))-&gt;getName();<br /> //Result : Drupal\mymodule\Tools\MyTools</code></p> <p>Methide 2.<br /> This will retuen the full clas name including namespace but not very usefull as already you have the object.</p> <p><code>$class_name = MyToolsBase::class;<br /> //Result : Drupal\mymodule\Tools\MyToolsBase</code></p> <h2>Get the class name of the currect PHP object;</h2> <p>Methode 1.</p> <p><code>$class_name = __CLASS__;<br /> //Result : Drupal\mymodule\Tools\MyTools</code></p> <p>Methode 2.</p> <p><code>$class_name = (new \ReflectionClass($this))-&gt;getName();<br /> //Result : Drupal\mymodule\Tools\MyTools</code></p> <p>Methode 3.</p> <p><code>$class_name = static::class;<br /> //Result : MyTools</code></p> <p>Methode 4.</p> <p><code>$class_name = self::class;<br /> //Result : MyToolsBases</code></p> <p> </p> <h2>Get class name without namespace on PHP.</h2> <p>Methode 1. (This methode is very fast)</p> <p><code>$class_name = (new \ReflectionClass($this))-&gt;getShortName();<br /> //Result : MyTools</code></p> <p>Methode 2.</p> <p><code>$class_name = array_pop(explode('\\', __CLASS__));<br /> //Result : MyToolsBase</code></p> <p>Methode 2.</p> <p><code>$class_name = array_pop(explode('\\', __CLASS__));<br /> //Result : MyToolsBase</code></p> <p>Methode 3.</p> <p><code>$class_name = array_pop(explode('\\', self::class));<br /> //Result : MyToolsBase</code></p> <p>Methode 4.</p> <p><code>$class_name = array_pop(explode('\\', static::class));<br /> //Result : MyTools</code></p> <p> </p> <h2>The PHP Object (POO) Reflection Class</h2> <p><strong>ReflectionClass</strong></p> <p><code>$object = (new \ReflectionClass($this));</code></p> <h3>Get the Doc comments</h3> <p><code>$value = (new \ReflectionClass($this))-&gt;getDocComment();</code></p> <h3>Get file name</h3> <p><code>$value = (new \ReflectionClass($this))-&gt;getFileName();</code></p> <h3>Get the namespace</h3> <p><code>$value = (new \ReflectionClass($this))-&gt;getNamespaceName()</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/module" hreflang="en">Module</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/php" hreflang="en">PHP</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/299/php-oop-examples-and-tips" hreflang="en">Php OOP - Examples and Tips</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=321&amp;2=comment&amp;3=comment" token="4A3C-vM4cdC_14MqYTB1IonrPYlaG3HAuxsfWRiZcqE"></drupal-render-placeholder> </section> Sat, 10 Jun 2017 15:14:15 +0000 editor 321 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/321/php-get-class-name#comments Download and Install drupal 8. http://drupal8.ovh/en/tutoriels/320/download-and-install-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Download and Install 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 Download and Install 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, 06/09/2017 - 13:44</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Requirements:</h2> <p>1. A web server (Apache or another web server)<br /> 2. Database (Mysql, MariaDB or SQLite)<br /> 3. <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="057496cb-b607-482f-9b3f-daccc28821b2" href="/en/content/191/how_install_php_5-5-9-or-later-on-linux-for-drupal-8" rel="nofollow">PHP </a>(Minimum 5.5.9)</p> <p><strong>For the developpement:</strong><br />     On windows : <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="1572b2f9-6c64-4bdb-ba10-8549bb4a562e" href="/en/tutoriels/200/install-wamp-on-windows" rel="nofollow">WAMP</a>, Docker<br />     On <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="5aea83a4-8a89-461a-a59b-106cb250868a" href="/en/content/190/install-lamp-on-linux" rel="nofollow">linux LAMP stack</a><br />     On Mac <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="d7d27ebf-38d0-43a9-a315-76d05b5cfa8b" href="/en/content/199/lamp-mamp-and-wamp" rel="nofollow">MAMP</a></p> <p> Note : It is recomended to install drush (Command line shell and Unix scripting interface for Drupal).<br /> - <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="9dcc0341-e791-4f89-8c34-5e3aab596250" href="/en/content/189/install-drush-on-linux" rel="nofollow">Install Drush on Linux</a><br /> -<a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="0f3ebe3d-38ea-4a6c-ad97-3c0e0d5bb208" href="/en/content/188/install-drush-on-widows" rel="nofollow"> Install Drush on Windows.</a></p> <p>Drupal has composer repository, It is recomended to use composer.<br /> - Install Composer on Windows<br /> - <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="2dac87a1-d882-41c7-815e-a12dce5c74eb" href="/en/tutoriels/202/install-and-update-composer" rel="nofollow">Install Composer on Linux</a></p> <h2> Download Drupal 8.</h2> <p>To get the drupal 8 codes, you can use one of the three methods. Download via composer is recomended because composer get automaticly all require dependencies (Of drupal core or contrib modules).</p> <h3>Download drupal via composer. (Recomended methode)</h3> <p><code># Run this command from your web server (or Virtual Machine / Virtual Host) public directory.<br /> composer create-project drupal/drupal drupal8</code><br /> This will download drupal core and other libreries than in to drupal8 folder.</p> <h3>Download drupal 8 via drush</h3> <p>This method is suitable if you don't want to use composer.<br /><code># Run this command from your web server (or Virtual Machine / Virtual Host) public directory.<br /> drush dl drupal -y</code></p> <h3>Manuel download from web.</h3> <p>Link : https://www.drupal.org/project/drupal<br /> Download the<strong> 8.X.X</strong> version then uncompress to your web server (or Virtual Machine / Virtual Host) public directory.</p> <h2> Install Drupal 8</h2> <p>Before install drupal on your server, you need to create a database (MySQL/MariaDB or PostgreSQL). If you haven't a database server, you can also use SQLite.</p> <h3>Install Drupal via drush (Recomended).</h3> <p><code># Using Mysql Database<br /> # For the example, mysql database User:drupal, Password:drupal Database:drupal<br /> drush site-install standard --db-url='mysql://drupal:poivrinedrupal@localhost/drupal' --site-name=Drupal --account-name=admin --account-pass=admin  --notify=0 --account-mail=name@mail.com -y</code></p> <p><code># Using a SQLite database (Not Recomended).<br /> drush site-install standard --db-url='sqlite://sites/default/files/.ht.db.sqlite' --site-name=Drupal8 --account-name=admin --account-pass=admin --notify=0 --account-mail=name@mail.com -y</code></p> <h3> Install Drupal from Web Interface.</h3> <p>Open web browser and visit the new drupal directory. You will automaticly redirect to the instalation page. Just follow the instruction on the page.<br /> You will select the database, Enter the database details and administration details.</p> <h2> Download and Install modules on drupal 8.</h2> <h3> Download contrib modules via composer.</h3> <p>Download via composer is recomended because composer get automaticly all require dependencies of the modules.<br /><code># Example. Module : Address<br /> # Download.<br /> composer require "drupal/address ~1.0"<br /> # Then Install via drush.<br /> drush en address -y</code></p> <h3>Download and install via Drush.</h3> <p>Note : drush will download any modules set as dependencies but not external libreries.<br /><code># Example : Module Admin Toolbar<br /> drush en admin_toolbar admin_toolbar_tools -y</code></p> <h3> Manuel download from web and install via web browser (This method will NOT download dependencies automaticly).</h3> <p>1. Download the modul and extract to drupalroot/modules/contrib<br /> 2. Go to /admin/modules (Extend)<br /> 3. Select the module and click on 'Install'</p> <h2> Few usefull contrib modules</h2> <h3> Enable few usefull contrib modules.</h3> <p><code>drush en admin_toolbar admin_toolbar_tools youtube imce linkit captcha honeypot securelogin pathauto -y</code></p> <h3> Enable drupal development modules.</h3> <p><code>drush en devel kint coffee -y</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/linux" hreflang="en">Linux</a></li> <li><a href="/en/tags/windows" hreflang="en">Windows</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/202/install-and-update-composer" hreflang="en">Install and Update Composer</a></div> <div class="field__item"><a href="/en/tutoriels/138/install-and-update-drush-on-linux" hreflang="en">Install and Update Drush on Linux</a></div> <div class="field__item"><a href="/en/tutoriels/99/install-drupal-8" hreflang="en">Install Drupal 8 - Requirements</a></div> <div class="field__item"><a href="/en/tutoriels/258/install-apache-2-ssl-on-debian-based-linux" hreflang="en">Install Apache 2 with SSL On Debian based Linux</a></div> <div class="field__item"><a href="/en/tutoriels/177/install-lamp-on-debian-based-linux" hreflang="en">Install LAMP On Debian based Linux</a></div> <div class="field__item"><a href="/en/tutoriels/201/install-mamp-apache-mysql-php-on-mac" hreflang="en">Install MAMP. Apache MySQL PHP on Mac</a></div> <div class="field__item"><a href="/en/tutoriels/9/install-drupal-8-virtualmin-error-500" hreflang="en">Install Drupal 8 with Virtualmin - Error 500</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=320&amp;2=comment&amp;3=comment" token="ytYqUPkzD4tdnrnHsL3gaueArDJ35mw3jI4w81JDCZc"></drupal-render-placeholder> </section> Fri, 09 Jun 2017 11:44:47 +0000 editor 320 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/320/download-and-install-drupal-8#comments Drupal 8 Plugin API http://drupal8.ovh/en/tutoriels/319/drupal-8-plugin-api <span class="field field--name-title field--type-string field--label-hidden">Drupal 8 Plugin 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 drupal 8  Plugin API ?</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, 06/07/2017 - 19:55</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Drupal 8 embedded with a plugin system. In drupal 8, Plugin is a group of PHP classes used for a defined specific usage. You can use any registered  plugins, add new plugins and also create your own plugin type.</p> <p>This tutorial show how to create your own drupal 8 plugin type with your ows plugins manager.</p> <h2>Create a custom module.</h2> <p>(Here, the module name is 'mymodule'). <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="b1e18681-bcf2-4efc-bf00-d4f5d9946bbe" href="/en/tutoriels/3/create-a-simple-module-for-drupal-8" rel="nofollow">Click here to know how to create a module.</a></p> <h2>Create Plugin interface.</h2> <p>(Here : FoodPluginInterface.php)<br /> Example :<br /><code>&lt;?php<br /> namespace Drupal\mymodule;<br /> /**<br />  * This is the plugin interface, to define all plugins methods.<br />  */<br /> interface FoodPluginInterface {<br />   /**<br />    * Get the plugin label.<br />    * @return string<br />    */<br />   public function label();<br />   /**<br />    * Return a text.<br />    * @return string<br />    */<br />   public function getText();<br /> }</code></p> <h2>Create Annotation class.</h2> <p>(Here : Annotation/Food.php)<br /> Example :<br /><code>&lt;?php<br /> namespace Drupal\mymodule\Annotation;<br /> use Drupal\Component\Annotation\Plugin;<br /> /**<br />  * Defines a Food annotation object.<br />  *<br />  * @ingroup food_api<br />  *<br />  * @Annotation<br />  */<br /> class Food extends Plugin {<br />   /**<br />    * The plugin ID.<br />    *<br />    * @var string<br />    */<br />   public $id;<br />   /**<br />    * The administrative label of the block.<br />    *<br />    * @var \Drupal\Core\Annotation\Translation<br />    *<br />    * @ingroup plugin_translatable<br />    */<br />   public $admin_label = '';<br /> }</code></p> <h2>Create Plugin Manager</h2> <p>(Here : FoodPluginManager.php) and use FoodPluginInterface and Annotation class.<br /> Example :<br /><code>&lt;?php<br /> namespace Drupal\mymodule\Plugin;<br /> use Drupal\Component\Plugin\Factory\DefaultFactory;<br /> use Drupal\Core\Cache\CacheBackendInterface;<br /> use Drupal\Core\Extension\ModuleHandlerInterface;<br /> use Drupal\Core\Plugin\DefaultPluginManager;<br /> /**<br />  * Provides a Food plugin manager.<br />  */<br /> class FoodPluginManager extends DefaultPluginManager {<br />   /**<br />    * Constructs a new FoodPluginManager object.<br />    */<br />   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {<br />     //parent::__construct('Plugin/Food', $namespaces, $module_handler, NULL, 'Drupal\mymodule\Annotation\Food');<br />     parent::__construct('Plugin/Food',$namespaces,$module_handler,'Drupal\mymodule\FoodPluginInterface','Drupal\mymodule\Annotation\Food');<br />     $this-&gt;alterInfo('food_info');<br />     $this-&gt;setCacheBackend($cache_backend, 'food_info_plugins');<br />     $this-&gt;factory = new DefaultFactory($this-&gt;getDiscovery());<br />   }<br /> }</code></p> <h2>Create a plugin manager service.</h2> <p>(Here : plugin.manager.food in mymodule.services.yml) and use the FoodPluginManager<br /> Example :<br /><code>services:<br />   plugin.manager.food:<br />     class: Drupal\mymodule\Plugin\FoodPluginManager<br />     parent: default_plugin_manager</code></p> <p><strong>Now your plugin type is ready to use.</strong> To create plugins esily, we will use a PluginBase abstract class (Here : FoodBase) then extend this base class.<br /> Example (Base class : FoodBase)<br /><code>&lt;?php<br /> namespace Drupal\mymodule\Plugin;<br /> use Drupal\Core\Plugin\PluginBase;<br /> use Drupal\mymodule\FoodPluginInterface;<br /> /**<br />  * Defines a base food plugin implementation that plugins will extend.<br />  *<br />  * @ingroup food_api<br />  */<br /> abstract class FoodBase extends PluginBase implements FoodPluginInterface {<br />   /**<br />    * {@inheritdoc}<br />    */<br />   public function label() {<br />     $definition = $this-&gt;getPluginDefinition();<br />     return (string) $definition['admin_label'];<br />   }<br /> }</code></p> <p>Example: (Class: Bread.php)<br /><code>&lt;?php<br /> namespace Drupal\mymodule\Plugin\Food;<br /> use Drupal\mymodule\Plugin\FoodBase;<br /> /**<br />  * Defines a fallback plugin for missing food plugins.<br />  *<br />  * @Food(<br />  *   id = "cake",<br />  *   admin_label = @Translation("Cake"),<br />  * )<br />  */<br /> class Cake extends FoodBase {<br />   /**<br />    * Return a text.<br />    * @return string<br />    */<br />   public function getText() {<br />     return "This is Cake plugin";<br />   }<br /> }</code></p> <p> </p> <h2>Usage / Use case.</h2> <p>Now using the plugins manager service, you can get and use all plugins of this type.<br /> Example:<br /> // Get all plugins of the type.<br /><code>$type = \Drupal::service('plugin.manager.food');<br /> $plugin_definitions = $type-&gt;getDefinitions();</code></p> <p>// Get a specific type by ID.<br /><code>\Drupal::service('plugin.manager.food')-&gt;getDefinition('cake');</code></p> <p>// Finally, use the plugin.<br /><code>$type = \Drupal::service('plugin.manager.food');<br /> $plugin = $type-&gt;createInstance('cake');<br /> $result = $plugin-&gt;getText();</code></p> <p>// Note : You can also get the manager like:<br /><code>$type = new FoodPluginManager(\Drupal::getContainer()-&gt;getParameter('container.namespaces'));</code></p> <p> </p> <p>Drupal Tutorials:<br /> https://www.drupal.org/docs/8/api/plugin-api/plugin-api-overview<br /> https://www.drupal.org/docs/8/api/plugin-api/creating-your-own-plugin-manager</p> </div> <div class="field field--name-field-file field--type-file field--label-inline"> <div class="field__label">File</div> <div class="field__item"> <span class="file file--mime-application-zip file--package-x-generic"> <a href="http://drupal8.ovh/sites/drupal/files/2017-06/mymodule_tuto_plugin_01.zip" type="application/zip; length=5573">mymodule_tuto_plugin_01.zip</a></span> </div> </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/plugin" hreflang="en">Plugin</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/186/create-a-new-plugin-type-using-drupal-8-plugin-api" hreflang="en">Create a new Plugin type using Drupal 8 Plugin 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=319&amp;2=comment&amp;3=comment" token="TmnKd8NSVFS82qrkeHR7BaLBIWtfNt4pK96YbIOZUt8"></drupal-render-placeholder> </section> Wed, 07 Jun 2017 17:55:02 +0000 editor 319 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/319/drupal-8-plugin-api#comments Test modules using drupal "Testing" System. http://drupal8.ovh/en/tutoriels/318/test-modules-using-drupal-testing-system <span class="field field--name-title field--type-string field--label-hidden">Test modules using drupal &quot;Testing&quot; System.</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 Testing module (previusly SimpleTest on Drupal 7) ?</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, 06/07/2017 - 17:55</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>SimpleTest (Testing) allow to test your drupal systems in a safe way. It's also allow you to check functionaleties, result ... of your module.</p> <p>First of all you must active Testing module on your envirenement.<br /> #Drush command:<br /><code>drush en simpletest -y</code></p> <h2>Create a webTest.</h2> <p>Just for example, do nothing.<br /> File : YOUR_MODULE/src/Tests/JustTest.php</p> <p><code>&lt;?php<br /> namespace Drupal\YOUR_MODULE\Tests;<br /> use Drupal\simpletest\WebTestBase;<br /> /**<br />  * Test drupal home page.<br />  *<br />  * @group Simple Analytics<br />  */<br /> class JustTest extends WebTestBase {<br />   // Check the text 'welcome' and 'drupal' on Home page.<br />   public function testJustTest() {<br />     $this-&gt;drupalGet('');<br />     $this-&gt;assertRaw('welcome', 'The page contains "welcome".');<br />     $this-&gt;assertRaw('drupal');<br />   }<br /> }</code></p> <h2>Run the test</h2> <p>1. By drupal Interface.<br /> Goto Configuration &gt; Development &gt; Testing<br /> Select your test (JustTest.php) and Run</p> <p>2. By  command line interface.</p> <p>Examples:<br /><code>php core/scripts/run-tests.sh --browser --class "Drupal\YOUR_MODULE\Tests\JustTest"<br /> php core/scripts/run-tests.sh --verbose --color --class "Drupal\YOUR_MODULE\Tests\JustTest"<br /> php core/scripts/run-tests.sh --verbose --sqlite test.sqlite --class "Drupal\YOUR_MODULE\Tests\JustTest"<br /> php core/scripts/run-tests.sh --verbose  --class "Drupal\YOUR_MODULE\Tests\JustTest"<br /> php core/scripts/run-tests.sh --verbose --browser --class "Drupal\YOUR_MODULE\Tests\JustTest"</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/tests" hreflang="en">Tests</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/269/simpletest-running-phpunit-tests" hreflang="en">SimpleTest : Running PHPUnit tests from command line</a></div> <div class="field__item"><a href="/en/tutoriels/123/simpletest-drupal-8" hreflang="en">SimpleTest With 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=318&amp;2=comment&amp;3=comment" token="31bwrzZBZPoa3-6UMywKNKKTKGEwRkoFiMg0HiBhFeo"></drupal-render-placeholder> </section> Wed, 07 Jun 2017 15:55:46 +0000 editor 318 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/318/test-modules-using-drupal-testing-system#comments Drupal 8 Module Settings and Mapping http://drupal8.ovh/en/tutoriels/316/drupal-8-module-settings-and-mapping <span class="field field--name-title field--type-string field--label-hidden">Drupal 8 Module Settings and Mapping</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 define default configuration values / Settings of a drupal 8 module ?<br /> How to set configuration values at the module installation ?</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, 06/07/2017 - 11:24</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>For the examples, here we use the module simple_analytics as example.<br /> Module name : simple_analytics<br /> Configuration object: simple_analytics.settings</p> <p>Settings and mapping files structure.</p> <p>MODULE-ROOT<br /> |-- config<br /> |    |-- install<br /> |        |-- MYMODULE.CONFIGURATION.yml<br /> |    |-- schema<br /> |        |-- MYMODULE.schema.yml<br /> .<br /> .<br /> .</p> <p>// Example. (simple_analytics)<br /> simple_analytics<br /> |-- config<br /> |    |-- install<br /> |        |-- simple_analytics.settings.yml<br /> |    |-- schema<br /> |        |-- simple_analytics.schema.yml<br /> .<br /> .<br /> .</p> <p> Settings file.</p> <p>This file contains the default settings of the module.<br /> Example: (config/install/simple_analytics.settings.yml)<br /> # Init settings for simple_analytics module.<br /> sa_tracker: true<br /> track_auth: true<br /> track_admin: false<br /> displaystat: 30<br /> google-id: ''<br /> piwik-id: ''<br /> custom: ''<br /> piwik-uri: ''<br /> # External libs.<br /> lib_chartist: false</p> <p>Mapping file</p> <p>This file define data structure (mapping and data types)<br /> Example : (config/schema/simple_analytics.schema.yml)<br /> # Schema for the configuration files of the simple_analytics module.<br /> simple_analytics.settings:<br />   type: config_object<br />   label: 'Simple Analytics settings'<br />   mapping:<br />     sa_tracker:<br />       type: boolean<br />       label: 'Internal tracker ON / OFF'<br />     track_auth:<br />       type: boolean<br />       label: 'Track authenticated users'<br />     track_admin:<br />       type: boolean<br />       label: 'Track admin pages'<br />     lib_chartist:<br />       type: boolean<br />       label: 'Chartist-JS lib present and activates'<br />     displaystat:<br />       type: integer<br />       label: 'Duration of the statistic page'<br />     google-id:<br />       type: string<br />       label: 'Google analytics ID'<br />     piwik-uri:<br />       type: string<br />       label: 'Piwik analytics URL'<br />     piwik-id:<br />       type: string<br />       label: 'Piwik analytics ID'<br />     custom:<br />       type: string<br />       label: 'Custom tracking code'</p> <p>      <br /> Data types</p> <p>config_object    (As an array)<br /> boolean<br /> email<br /> integer<br /> float<br /> string<br /> uri</p> <p> Fore more details see Drupal Doc :<br /> https://www.drupal.org/docs/8/api/configuration-api/configuration-schemametadata</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/315/drupal-configuration-system-config-api" hreflang="en">Drupal Configuration System (Config API)</a></div> <div class="field__item"><a href="/en/tutoriels/33/set-configuration-at-module-installation" hreflang="en">Set configuration at the module installation</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=316&amp;2=comment&amp;3=comment" token="kXMmdnL_2kROzd_OtpgQynqVQf_tikb8xCGTwQZekCE"></drupal-render-placeholder> </section> Wed, 07 Jun 2017 09:24:30 +0000 editor 316 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/316/drupal-8-module-settings-and-mapping#comments Drupal Configuration System (Config API) http://drupal8.ovh/en/tutoriels/315/drupal-configuration-system-config-api <span class="field field--name-title field--type-string field--label-hidden">Drupal Configuration System (Config 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 Drupal 8 config API (The configuration system)?</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, 06/07/2017 - 11:05</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Drupal 8 embedded with a configuration system which allow users to store variables in the database and usable in site wide as variables in drupal 7. Intend of use variable_set variable_get variable_del, in drupal 8 use Configuration API. Drupal provide 2 methods to use configuration in Readable and Writable mode.<br /> To write/edit (as variable_set and variable_del) a configuration:  \Drupal::configFactory()-&gt;getEditable('THE-CONFIG-NAME')<br /> To read (as variable_get) a config : \Drupal::config('THE-CONFIG-NAME')<br /> Examples:</p> <p>Editable mode (Can also read).</p> <p>$config = \Drupal::configFactory()-&gt;getEditable('my_config_name.config'); // Get config object.<br /> $config-&gt;set('test_key','test_value'); // Set value.<br /> $config-&gt;save(); // Save configuration.<br /> // Few other operations.<br /> $config-&gt;delete(); // Delete the configuration object.<br /> $config-&gt;clear('test_key'); // Clear a value.<br /> $config-&gt;merge($data_array); // Merge values.<br /> $value = $config-&gt;get('test_key'); // Read a value.<br /> $config-&gt;save(); // Save configuration.</p> <p>Readable mode.</p> <p>$config = \Drupal::config('my_config_name.config');<br /> $value = $config-&gt;get('test_key');</p> <p>Note: Using a correct naming convention avoid conflicts between modules. It is recommended to use configuration name like:<br /> THE_MODULE_NAME.CONFIG<br /> Example: mymodule.settings</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/33/set-configuration-at-module-installation" hreflang="en">Set configuration at the module installation</a></div> <div class="field__item"><a href="/en/tutoriels/316/drupal-8-module-settings-and-mapping" hreflang="en">Drupal 8 Module Settings and Mapping</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=315&amp;2=comment&amp;3=comment" token="oeRqenhOwgTD1HBL9Z32Y_legSc19PvhYaf2Qf-8cnI"></drupal-render-placeholder> </section> Wed, 07 Jun 2017 09:05:31 +0000 editor 315 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/315/drupal-configuration-system-config-api#comments