Module http://drupal8.ovh/en/tags/module en Drupal Commerce - e-commerce with Drupal http://drupal8.ovh/en/tutoriels/342/drupal-commerce-e-commerce-drupal <span class="field field--name-title field--type-string field--label-hidden">Drupal Commerce - e-commerce with Drupal</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to create a e-shop useing drupal 8 and drupal commerce ?</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, 09/27/2017 - 11:29</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p><strong>CAUTION : At this day (2017-10-12), Drupal commerce is very young. Therefor you can start the development of your e-commerce site but you may have bugs and security issues.</strong></p> <h3>Official Sites</h3> <ul><li>https://www.drupal.org/project/commerce</li> <li>https://drupalcommerce.org/</li> </ul><h2> Install instructions</h2> <ul><li>https://docs.drupalcommerce.org/commerce2/developer-guide/install-update/installation</li> </ul><p><em>You must get drupal commerce using <strong>composer</strong>, do not download directly from drupal.org or via drush.</em></p> <h3>New site</h3> <p><code>composer create-project drupalcommerce/project-base mystore --stability dev</code></p> <h3>Existing site</h3> <p><code>composer require "drupal/commerce"<br /> drupal module:install commerce_product commerce_checkout commerce_cart<br /> #OR<br /> drush en commerce_product commerce_checkout commerce_cart</code></p> <h2> Payment methods (modules)</h2> <p>Few payments modules are already developed and listed on this site.</p> <p>https://fr.commerceguys.com/en/blog/focus-on-payments-modules-for-drupal-commerce</p> <h3> Currently available payment methods and modules</h3> <ul><li>Braintree</li> <li>Authorize.net</li> <li>Paymill</li> <li>Stripe</li> <li>Paypal</li> <li>Ingenico</li> <li>Vantiv</li> <li>PromisePay</li> <li>Worldline</li> <li>Wechat Pay</li> <li>Commerce offlines payments</li> </ul><p> </p> <p><strong>Download Paypal module for drupal commerce 2:</strong><br /> Github  : https://github.com/drupalcommerce/commerce_paypal</p> <p>Payment gateways development status<br /> https://docs.google.com/spreadsheets/d/1efOOTDFqweCkAnPkIElEbx4yavXoVZjVlJF1fIbtrj8/edit#gid=1454227332</p> <p> Step 2 : TODO : How to setup a drupal commerce site.</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/module" hreflang="en">Module</a></li> <li><a href="/en/tags/commerce" hreflang="en">Commerce</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=342&amp;2=comment&amp;3=comment" token="S14DodYms5JfMGxiRPc-kL9BEU6-h9LHiKGCr84wTAw"></drupal-render-placeholder> </section> Wed, 27 Sep 2017 09:29:43 +0000 editor 342 at http://drupal8.ovh Use IMCE on custom page http://drupal8.ovh/en/tutoriels/341/use-imce-on-custom-page <span class="field field--name-title field--type-string field--label-hidden">Use IMCE on custom page</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 insert IMCE file browser to a custom form or a custom page.</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, 09/18/2017 - 16:52</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Example 1 (for drupal 7)</h2> <p> Step 1. Add file brows</p> <p>Create a simple js file in your module directory (called yourModuleName.js) and enter the following Javascript.</p> <p><code>function openFileBrowser() {<br />   window.open('/imce?app=yourApp|url@your-input-id', '', 'width=760,height=560,resizable=1');<br /> }</code></p> <p>2) Make sure to include your js file:</p> <p><code>$path = drupal_get_path('module', 'yourModuleName');<br /> drupal_add_js($path . '/yourModuleName.js');</code></p> <p> 3) Create the form element where you want the path to the file to be generated:</p> <p><code>$form['your-input-name'] = array(   <br />   '#title' =&gt; t('Your Title'),  <br />   '#type' =&gt; 'textfield',   <br />   '#attributes' =&gt; array('readonly' =&gt; 'readonly'), // So that the user cannot manually enter a value  <br /> &gt; );</code></p> <p> </p> <p>4) Create a button that will execute the Javascript function:</p> <p><code>$form['your-button-name'] = array(<br />   '#type' =&gt; 'submit',<br />   '#value' =&gt; t('Browse Server'),<br />   '#attributes' =&gt; array('onclick' =&gt; 'openFileBrowser();'),<br /> );</code></p> <h2> Example 2 using inline JS (for drupal 7)</h2> <p><code>$inlinejs = &lt;&lt;&lt;JS<br /> function openFileBrowser() {<br />   window.open('/imce?app=yourApp|url@file-input-id', '', 'width=760,height=560,resizable=1');<br /> }<br /> JS;<br /> drupal_add_js($inlinejs, ['type' =&gt; 'inline', 'scope' =&gt; 'footer', 'weight' =&gt; 5]);</code></p> <p><code>$output = [];<br /> $output['open-imce-btn'] = [<br />   '#type' =&gt; 'submit',<br />   '#value' =&gt; t('Open IMCE'),<br />   '#attributes' =&gt; ['onclick' =&gt; 'openFileBrowser();'],<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> <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> <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=341&amp;2=comment&amp;3=comment" token="CF9Tjt6RGi5TBYoA0WONO5fMLP7Q_YSOsB4RgjcFuuI"></drupal-render-placeholder> </section> Mon, 18 Sep 2017 14:52:29 +0000 editor 341 at http://drupal8.ovh Disable cache for a specific page http://drupal8.ovh/en/tutoriels/340/disable-cache-for-a-specific-page <span class="field field--name-title field--type-string field--label-hidden">Disable cache for a specific page</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"><ul><li>How to disable cache for a custom page ?</li> <li>How to disable cache for a specific content type ?</li> </ul></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">Sun, 09/17/2017 - 12:57</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Disable cache for a custom page from route declaration.</h2> <p>If you want to disable cache for a custom controller (Custom module), You have no_cache option (YOUR_MODULE.routing.yml).<br /> Example :<br /> File : <strong>mymodule.routing.yml</strong></p> <p><code>mymodule.myroute:<br />   path: '/mymodule/mypage'<br />   defaults:<br />     _controller: '\Drupal\mymodule\Controller\Pages::mypage'<br />     _title: 'No cache page'<br />   requirements:<br />     _access: 'TRUE'<br /><strong>  options:<br />     no_cache: 'TRUE'</strong></code></p> <p> </p> <h2>Disable cache for a specific content type</h2> <p>Example:</p> <p><code>function yourmodule_node_view(array &amp;$build, NodeInterface $node, $display, $view_mode) {<br />   if ($node-&gt;getType() == 'yourtype' &amp;&amp; $view_mode == 'full') {<br />     $build['#cache']['max-age'] = 0;<br />   }<br /> }</code></p> <p> </p> <h2>Disable cache from a controller.</h2> <p>Note : This will work in no cache on page level (not working for anonymous users for example).</p> <p>Set cache max age on rederer array.</p> <p><code>$output = [];<br /> $output['#cache']['max-age'] = 0;</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/cache" hreflang="en">Cache</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/256/drupal-8-cache-api" hreflang="en">Drupal 8 Cache API</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=340&amp;2=comment&amp;3=comment" token="DfyS8E8xjrzoi0hi3ayYL2JyzetK0_DZ_QHGef6QIv4"></drupal-render-placeholder> </section> Sun, 17 Sep 2017 10:57:43 +0000 editor 340 at http://drupal8.ovh How to create a JSON web service on drupal 8 ? http://drupal8.ovh/en/tutoriels/335/how-to-create-a-json-web-service-on-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">How to create a JSON web service 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 JSON web service with Drupal 8 ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Tue, 07/25/2017 - 10:33</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>This is a simple example of a web service. For the example, We will make a service to multiply two numbers.</p> <h2>Create JSON WebService module</h2> <h3>Create a module.</h3> <p>Here :<strong> mywebservice</strong></p> <h3>Create route, the Web Service path.</h3> <p>Example:<br /><code>mywebservice.multiply:<br />   path: '/mywebservice/maths/multiply'<br />   defaults:<br />     _controller: '\Drupal\mywebservice\Controller\MyWebService::multiply'<br />     _title: 'MyWebService'<br />   requirements:<br />     _permission: 'access content'</code></p> <h3> Create webservice Controller</h3> <p>Example:<br /><code>&lt;?php<br /> //<br /> namespace Drupal\mywebservice\Controller;<br /> //<br /> use Drupal\Core\Controller\ControllerBase;<br /> use Symfony\Component\HttpFoundation\JsonResponse;<br /> /**<br />  * Class MyWebService.<br />  */<br /> class MyWebService extends ControllerBase {<br />   /**<br />    * Multiply.<br />    */<br />   public function multiply() {<br />     $request = \Drupal::request();<br />     $output['a'] = $request-&gt;get('a');<br />     $output['b'] = $request-&gt;get('b');<br />     $output['result'] = $output['a'] * $output['b'];<br />     return new JsonResponse($output);<br />   }<br /> }</code></p> <h2>Test the web service</h2> <p>Visit : http://yourdomain.loc/<strong>mywebservice/maths/multiply</strong><br /> Must return a JSON : <code>{"a":null,"b":null,"result":0}</code></p> <p>Visit : http://yourdomain.loc/<strong>mywebservice/maths/multiply?a=5&amp;b=2</strong><br /> Must return a JSON : <code>{"a":"5","b":"2","result":10}</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-07/mywebservice.zip" type="application/zip; length=2485">mywebservice.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/module" hreflang="en">Module</a></li> <li><a href="/en/tags/webservice" hreflang="en">WebService</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/251/drupal-8-restful-web-services" hreflang="en">Drupal 8 RESTful Web Services</a></div> <div class="field__item"><a href="/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page" hreflang="en">Create a simple module for Drupal 8 - Step 2 - Create Simple Page</a></div> </div> <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=335&amp;2=comment&amp;3=comment" token="X6xyZNjr5Ah9SnOmJu5xg8DQwkvb08bFFHvmNdzCwLA"></drupal-render-placeholder> </section> Tue, 25 Jul 2017 08:33:14 +0000 editor 335 at http://drupal8.ovh Drupal coding Standard and Best Practice http://drupal8.ovh/en/tutoriels/327/drupal-coding-standard-and-best-practice <span class="field field--name-title field--type-string field--label-hidden">Drupal coding Standard and Best Practice</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 <strong>phpcs </strong>and <strong>phpcbf </strong>to check and correct<strong> drupal 8</strong> coding Standard ?</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, 06/29/2017 - 13:05</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><blockquote><p> Create a Drupal 8 module is a good idea, but develop as Drupal is BEST.</p> </blockquote> <p>The famous tools <strong>PhpCS </strong>and <strong>PhpCBF </strong>allow you to check and correct (some) drupal coding standards and best practice errors.</p> <h2>Install Drupal coder.</h2> <p>Step 1. Download drupal coder via drush (<strong>drush dl coder</strong>) or from <strong>https://www.drupal.org/project/coder</strong> and extract into a folder <strong>out side</strong> of the drupal project.<br /> Here, for the example : <strong>/home/myhome/coder</strong></p> <p>Step 2. Download / Install additional packages using composer. (<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">How to install composer</a>)<br /> Example:<br /><code>cd /home/myhome/coder<br /> composer update</code></p> <p>Step 3. Configure envirenement.<br /> Example:<br /><code>export PATH="$PATH:/home/myhome/coder/vendor/bin"<br /> set PATH $PATH /home/myhome/coder/vendor/bin<br /> phpcs --config-set installed_paths /home/myhome/coder/coder_sniffer<br /> phpcbf --config-set installed_paths /home/myhome/coder/coder_sniffer</code></p> <h2> Use PhpCS anf PhpCBF</h2> <p>-&gt; Go to your custom module directory. Example : /var/www/drupal8/modules/custom/mymodule<br /> You can use phpcs using absolute path of the module. But I got <strong><em>some errors when using phpcbf</em></strong>.<br /><strong>phpcs --standard=Drupal /var/www/drupal8/modules/custom/mymodule</strong></p> <p>Example of the Errors :<br /><code>Array<br /> (<br /> [0] =&gt; Ignoring potentially dangerous file name /var/www/drupal8/modules/custom/mymodule.....<br /> [1] =&gt; can't find file to patch at input line 3</code></p> <h3>Check drupal coding Standard errors</h3> <p><code>phpcs --standard=Drupal .<br /> OR<br /> phpcs --standard=Drupal /var/www/drupal8/modules/custom/mymodule</code></p> <h3>Correst some drupal coding Standard errors (Merked as [X])</h3> <p><code>phpcbf --standard=Drupal .<br /> OR<br /> # This may not work : To test<br /> phpcbf --standard=Drupal /var/www/drupal8/modules/custom/mymodule</code></p> <h3>Check drupal Best Practice errors</h3> <p><code>phpcs --standard=DrupalPractice .<br /> OR<br /> phpcs --standard=DrupalPractice /var/www/drupal8/modules/custom/mymodule</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/debug" hreflang="en">Debug</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/202/install-and-update-composer" hreflang="en">Install and Update Composer</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=327&amp;2=comment&amp;3=comment" token="PG_cMf-0dyBQ02oeI_IMcBhl6bm5fjx-Op9bqcyn62g"></drupal-render-placeholder> </section> Thu, 29 Jun 2017 11:05:53 +0000 editor 327 at http://drupal8.ovh 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> </p> <h2>Print Error, Warning, Success or OK message</h2> <p><code>drush_log('Test Success message','success');<br /> drush_log('Test Warning message','warning');<br /> drush_log('Test Error message','error');</code><br /><code>drush_log('Test OK message','ok');</code><br /><code>drush_log('Test Cancel message','cancel');</code><br />  <br />  <br /> Run a drush command (Print help command).</p> <p><code>drush_invoke('help', ['cex']);</code></p> <h2> User input - Prompt for user input in Drush</h2> <h3>Ask for options</h3> <p><code>$options = [<br /> 'Test' =&gt; t('Test'),<br /> '0' =&gt; t('Error'),<br /> ];<br /> $option = drush_choice($options, t('Please choose a option.'));</code></p> <h3> Ask Yes/No Confirmation</h3> <p>NOTE : If you set the option<code><strong> -y</strong></code>, this will return <strong><code>TRUE </code></strong>without prompting.</p> <p><code>if (drush_confirm('Are you sure you want printed \'Hello world\' to the screen ?')) {<br />   drush_print('Hello world !!!');<br /> }<br /> else {<br />   drush_user_abort();<br /> }</code></p> <h3> Ask for input value</h3> <p><code>$value = drush_prompt(dt('Please enter your name'));<br /> drush_print(dt('Hello @value!', ['@value' =&gt; $value]));</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 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