Drupal 7 http://drupal8.ovh/en/tags/drupal-7 en 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 Make Some operations Programmatically - Hook Update Deploy Tools http://drupal8.ovh/en/tutoriels/281/make-some-operations-programmatically-hook-update-deploy-tools <span class="field field--name-title field--type-string field--label-hidden">Make Some operations Programmatically - Hook Update Deploy Tools</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to make deploy operations programmatically ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Wed, 01/11/2017 - 11:33</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p><strong>Hook Update Deploy Tools</strong> is a great contrib module help to make most of site deploy operations programmatically.<br /> Link : https://www.drupal.org/project/hook_update_deploy_tools</p> <p>CAUTION : Current drupal 8 version is unstable (8.x-1.0-unstable1, 2016-Feb-07 Version),  but 8.x-1.x-dev version is usable.</p> <h3>Example: Delete a field instance.<br /> Using contrib module hook_update_deploy_tools</h3> <p>//Drupal 8:<br /> $message = \HookUpdateDeployTools\Fields::deleteInstance('taxonomy_term', 'field_name', 'tags');</p> <p>//Drupal 7:  <br /> $message = HookUpdateDeployTools\Fields::deleteInstance('taxonomy_term', 'field_name', 'tags');</p> <p> </p> <p>Without Hook Update Deploy Tools<br /> //Drupal 8:</p> <p>//Drupal 7:<br /> $instance = field_info_instance('node', 'field_name', 'page');//For node<br /> $instance = field_info_instance('taxonomy_term', 'field_name', 'tags');//For taxonomy term<br /> if ($instance) {<br />   field_delete_instance($instance);<br /> }</p> <p>//D8 (Not tested)<br /> $field_info = \Drupal\field\Entity\FieldConfig::loadByName('node', 'page', 'field_name');<br /> $field_info-&gt;delete();</p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/drupal-7" hreflang="en">Drupal 7</a></li> <li><a href="/en/tags/module" hreflang="en">Module</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/223/change-drupal-8-text-field-maximum-length" hreflang="en">Change Drupal 8 text field maximum length</a></div> <div class="field__item"><a href="/en/tutoriels/263/custom-content-entity-field-types-of-drupal-8" hreflang="en">Custom Content Entity Field types of drupal 8</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=281&amp;2=comment&amp;3=comment" token="KYveFfDzLjOT9ZYKKKT_YIMdXUoD0_RHvVkJlyx_iqI"></drupal-render-placeholder> </section> Wed, 11 Jan 2017 10:33:42 +0000 editor 281 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/281/make-some-operations-programmatically-hook-update-deploy-tools#comments Solr Configuration for Drupal http://drupal8.ovh/en/tutoriels/273/solr-configuration-for-drupal <span class="field field--name-title field--type-string field--label-hidden">Solr Configuration for 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 setup solr server for Drupal (Drupal 8 and 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">Tue, 12/13/2016 - 10:40</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Step 1. <a href="/en/tutoriels/272/install-solr-search-api" rel="nofollow">Install Tomcat And Solr Server (atlest 5.4.1)</a></p> <p>Step 2. Install Solr Search API On your drupal</p> <p>#Drush# drush en search_api_solr -y</p> <p>Step 3. Copy solr core files</p> <p>You have an example of "Solr configuration" files deleverd with drupal search_api_solr contrib module. on  <strong>search_api_solr/solr-conf/5.x. </strong>Copy this folder into the solr server core folder (Ex : /opt/solr-5.4.1/server/solr <a href="/en/tutoriels/272/install-solr-search-api" rel="nofollow">On our example</a>) and rename it (Ex: 5.x -&gt;<strong> new_core</strong>)</p> <p>Step 4. Add this core to sorl system.</p> <ul><li>Go to http://127.0.0.1:8983/solr/ (Or your server path) <ul><li>NOTE : If you use <strong>Jetty</strong> (as the example) server is at port <strong>8983</strong>, and Tomcat uses 8080 by default.</li> </ul></li> <li>Click on <strong>Core Admin</strong> -&gt; <strong>Add Core</strong> -&gt;Create New Core (Here core name is "<strong>new_core</strong>") <ul><li>If you have an error like ".../data/elevate.xml not found", copy <strong>new_core/elevate.xml</strong> file into <strong>new_core/<u>data</u>/elevate.xml</strong>.</li> </ul></li> </ul><p>Step 5. Setup Solr on drupal</p> <ul><li>Goto /admin/config/search/search_api -&gt; <strong>Add Server</strong></li> <li>Set basic data, (Name = solr_serveur ....)</li> <li>Select Service class = Solr service</li> <li>Configure Solr access <ul><li>Solr path may like /solr or /solr/CORENAME. (Ex: /solr/new_core)</li> </ul></li> <li>Create / Save Server.</li> </ul><p>Now you can view, edit, configure, index .... your solr from /admin/config/search/search_api/server/SOLR_SERVER_NAME (For the example : /admin/config/search/search_api/server/solr_serveur)</p> <p> </p> <h2>Tips : Few drush commands</h2> <p>Re Index contents<br /><code>drush sapi-r;drush sapi-i</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/module" hreflang="en">Module</a></li> <li><a href="/en/tags/drupal-7" hreflang="en">Drupal 7</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/284/create-a-advances-search-system-for-drupal-8-search-api" hreflang="en">Create an advances Search system for Drupal 8 with Search 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=273&amp;2=comment&amp;3=comment" token="YZkNGvuuNab3dv_BR_Q8ZnEaXtQJISCZjMiruc4cv34"></drupal-render-placeholder> </section> Tue, 13 Dec 2016 09:40:27 +0000 editor 273 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/273/solr-configuration-for-drupal#comments Set Drupal System variables manually http://drupal8.ovh/en/tutoriels/260/set-drupal-system-variables-manually <span class="field field--name-title field--type-string field--label-hidden">Set Drupal System variables manually</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 set Drupal System variables manually from a file (settings.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">Fri, 11/04/2016 - 11:45</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>To set Drupal System variables you can use the <strong>$settings (on Drupal 8)</strong> and <strong>$conf (On Drupal 7)</strong> array of<strong> setting.php</strong> file.</p> <h2> Examples.</h2> <h3>On Drupal 8</h3> <p><code>$settings['file_public_path'] = '/path/to/dir/public';</code></p> <h3>On Drupal 7</h3> <p><code>$conf['file_public_path'] = '/path/to/dir/public';<br /> $conf['theme_debug'] = 1;</code></p> <h3>Local settings</h3> <p>You can also set local changes using a separate settings file and put your local settings into this file like:</p> <p><code>if (file_exists(__DIR__ . '/settings.local.php')) {<br />    include __DIR__ . '/settings.local.php';<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/drupal-7" hreflang="en">Drupal 7</a></li> <li><a href="/en/tags/settings" hreflang="en">Settings</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=260&amp;2=comment&amp;3=comment" token="vFb8-iICYIh8a85JGjeyiCAXzX6jcB28pun_d-IrwGw"></drupal-render-placeholder> </section> Fri, 04 Nov 2016 10:45:13 +0000 editor 260 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/260/set-drupal-system-variables-manually#comments HTML Entity Decode and Encode http://drupal8.ovh/en/tutoriels/241/html-entity-decode-and-encode <span class="field field--name-title field--type-string field--label-hidden">HTML Entity Decode and Encode</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 Encode and decode HTML entitys 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, 10/04/2016 - 11:59</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Basic php and drupal 7 / 8 Examples</h2> <p>1.<br /><code>$string_out = html_entity_decode($string, ENT_QUOTES, 'UTF-8')</code><br /> //Do not forget ENT_QUOTES and 'UTF-8', otherwise quotes will not decodes.</p> <p>2.<br /><code>$string_out = check_plain($string)</code><br /> //Same as : <code>$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');</code></p> <p>3.</p> <h3>Transliterate</h3> <p><code>$string = "The string to transliterate";<br /> $trans = \Drupal::transliteration();<br /> $string = $trans-&gt;transliterate($string, 'en');</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/drupal-7" hreflang="en">Drupal 7</a></li> <li><a href="/en/tags/php" hreflang="en">PHP</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=241&amp;2=comment&amp;3=comment" token="e3CIFIFw65ZxCl2noDmTgMicHB44fVHQD-OJ5FgKwHM"></drupal-render-placeholder> </section> Tue, 04 Oct 2016 09:59:58 +0000 editor 241 at http://drupal8.ovh Execute a drush command programmatically http://drupal8.ovh/en/tutoriels/235/execute-a-drush-command-programmatically <span class="field field--name-title field--type-string field--label-hidden">Execute a drush command programmatically</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 execute a drush command programmatically ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Thu, 09/29/2016 - 15:49</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h3>Methodes:</h3> <p>drush_invoke (For the current site)<br /><code>drush_invoke($command, $arguments = array());</code></p> <p>drush_invoke_process<br /> This creates a new process in a new Drupal instance. There for you must specify the site alias.<br /><code>drush_invoke_process($site_alias_record, $command_name, $commandline_args = array(), $commandline_options = array(), $backend_options = TRUE);</code></p> <h3>Example 1. Enable a Module/Feature and Revert all features.</h3> <p><code>$site = drush_sitealias_get_record('@mysite');<br /> drush_invoke_process($site, 'pm-enable', array("features_dependencies","-y"));<br /> drush_invoke_process($site, 'features-revert-all', array("-y","--force"));</code></p> <p>//OR for the current site:<br /> drush_invoke_process('@self', 'features-revert-all', array("-y","--force"));<br />  </p> <p> </p> <p>Example 2. Run a drush command at  <span lang="en" xml:lang="en" xml:lang="en"><span>the installation of</span> <span>a module. </span></span></p> <p><code>function mymodule_install() {<br />     // clears the 'all' cache for current web site<br />     //Methode 1.<br />     drush_invoke('cache-clear', 'all');<br />     <br />     Or</code></p> <p><code>    //Methode2.<br />     $site = drush_sitealias_get_record('@mysite');<br />     drush_invoke_process($site, 'cache-clear', array('all'));<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/drush" hreflang="en">Drush</a></li> <li><a href="/en/tags/drupal-7" hreflang="en">Drupal 7</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/98/useful-drush-commands" hreflang="en">Useful Drush commands</a></div> <div class="field__item"><a href="/en/tutoriels/323/create-a-custom-drush-command-on-drupal-8" hreflang="en">Create a custom DRUSH command on drupal 8</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=235&amp;2=comment&amp;3=comment" token="hW2IOZmCmwFRCThT3QIYuPVZe3WfYbKntb8NOSMWcoA"></drupal-render-placeholder> </section> Thu, 29 Sep 2016 13:49:30 +0000 editor 235 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/235/execute-a-drush-command-programmatically#comments Change Date format on Views BEF http://drupal8.ovh/en/tutoriels/227/change-date-format-on-views-bef <span class="field field--name-title field--type-string field--label-hidden">Change Date format on Views BEF</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to change the date format on views Better exposed form and jQuery date popup ?</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, 08/29/2016 - 11:03</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>There are several methodes.</p> <p>Change date format Using custom module.</p> <p>1. <a href="/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page" rel="nofollow">Create a module</a> and use he hook <span>hook_form_views_exposed_form_alter(&amp;$form, &amp;$form_state);<br /> Example: </span><br /><code>function mymodule_form_views_exposed_form_alter(&amp;$form, &amp;$form_state){<br />   $date_format = "d-m-Y";<br />   $date_fields = array(<br />     'node_changed',<br />     'field_myfield_eventdate',<br />   );<br />   foreach ($date_fields as $field) {<br />     if (!empty($form[$field])) {<br />       if (!empty($form[$field]['#date_format'])) {<br />         $form[$field]['#date_format'] = $date_format;<br />       }<br />       if (!empty($form[$field]['min']['#date_format'])) {<br />         $form[$field]['min']['#date_format'] = $date_format;<br />       }<br />       if (!empty($form[$field]['max']['#date_format'])) {<br />         $form[$field]['max']['#date_format'] = $date_format;<br />       }<br />       if (!empty($form[$field]['value']['#date_format'])) {<br />         $form[$field]['value']['#date_format'] = $date_format;<br />       }<br />     }<br />   }<br /> }</code></p> <p>2. Change date popup format by JS/jQuery or by hook_js_alter(&amp;$js)<br /> 2.1 By hook_js_alter<br /><code>function mymodule_js_alter(&amp;$js) {<br />   $js['settings']['data'][] = array('better_exposed_filters'=&gt; array('bef_dateformat'=&gt;'dd-mm-yy'));<br /> }</code><br /> 2.2 By JS/jQuers : Add following js (<a href="/en/tutoriels/70/add-javascript-to-a-module" rel="nofollow">How to add a javascript</a>)<br /><code>$('.bef-datepicker').datepicker({ dateFormat: 'dd-mm-yy' });</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/drupal-7" hreflang="en">Drupal 7</a></li> <li><a href="/en/tags/views" hreflang="en">Views</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=227&amp;2=comment&amp;3=comment" token="tfKY6E_UC8oGrphE4MGd22HVFSktdTWCDXVwFmTdiOA"></drupal-render-placeholder> </section> Mon, 29 Aug 2016 09:03:21 +0000 editor 227 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/227/change-date-format-on-views-bef#comments Tips for Form API of Drupal 8 http://drupal8.ovh/en/tutoriels/225/form-api-of-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">Tips for Form API of 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>Some Tips of the drupal 8 form API.</p> <ul><li>How to add something just after a input ?</li> <li>How to make a collapsible field set ?</li> <li>How to Add a custom Validator / Submitter?</li> <li>How to use multiple submit buttons ?</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">Fri, 08/26/2016 - 14:01</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Add html after a field</h2> <p>(#field_prefix and #field_suffix).</p> <p>Example:<br /><code>$form['fieldname'] = array(<br />   '#type' =&gt; 'input',<br />   '#title' =&gt; t('The Label'),<br /><strong>  '#prefix' =&gt; '&lt;span&gt;Your HTML Just Before&lt;/span&gt;'<br />   '#suffix' =&gt; '&lt;span&gt;Your HTML Just After&lt;/span&gt;'</strong><br /> );</code></p> <p>Add HTML just after a input, select, textarea ... (#field_prefix and #field_suffix)</p> <p>Example:<br /><code>$form['fieldname'] = array(<br />   '#type' =&gt; 'input',<br />   '#title' =&gt; t('The Label'),<br /><strong>  '#field_prefix' =&gt; '&lt;span&gt;Your HTML Just Before input&lt;/span&gt;'<br />   '#field_suffix' =&gt; '&lt;span&gt;Your HTML Just After input&lt;/span&gt;'</strong><br /> );</code></p> <h2>Make a collapsible field set</h2> <p>Drupal 8 use HTML5  details tag and<strong> "OPEN" attribute</strong> to make a collapsible fieldset.  Example:<br /><code>$form['filters'] = array(<br />   '#type' =&gt; 'details',<br />   '#title' =&gt; 'Filters',<br />   '#open' =&gt; FALSE,<br /> );</code></p> <p>Add a custom Validator / Submitter</p> <p>Step 1. Create your Validator method</p> <p><code>public function <strong>validateFormCustom</strong>(array &amp;$form, FormStateInterface $form_state) {<br />   //Your validation codes<br /> }</code></p> <p>Step 2. Add this validator to your form.</p> <p><code>$form['#validate'][] = '::<strong>validateFormCustom</strong>';</code></p> <h2>Handle multiple submit buttons</h2> <p>Step 1. Create several submit buttons and add custom submitter to each buttons (If you add nothing, it use the default submit method):</p> <p><code>$form['actions']['submit_apply'] = [<br />   '#type' =&gt; 'submit',<br />   '#value' =&gt; t('Apply'),<br /> ];<br /> $form['actions']['submit_reset'] = [<br />   '#type' =&gt; 'submit',<br />   '#value' =&gt; t('Reset'),<br />   '#submit' =&gt; array('::submitFormReset'),<br /> ];</code></p> <p>Step 2. Then create your custom submit functions like:</p> <p><code>public function submitForm(array &amp;$form, FormStateInterface $form_state) {<br />   drupal_set_message("Apply");<br />   $form_state-&gt;setRebuild(TRUE);<br /> }<br /> public function submitFormReset(array &amp;$form, FormStateInterface $form_state) {<br />   drupal_set_message("Reset");<br />   $form_state-&gt;setRebuild(FALSE);<br /> }</code></p> <p> </p> <h3>Set Form elements as arrays in Drupal 8</h3> <p> Use the<strong> "#tree" =&gt; TRUE</strong> configuration.<br /><code>Example :<br />   $form["test_field"]["#tree"] = TRUE;<br />   $form["test_field"][] = array(<br />         "#type" =&gt; "textfield",<br />         "#title" =&gt; "Title 1",<br />     );<br />   $form["test_field"][] = array(<br />         "#type" =&gt; "textfield",<br />         "#title" =&gt; "Title 2",<br />     );</code></p> <p> </p> <h3>Add WYSIWYG CKEditor widget to a textarea</h3> <p><code>$form['fieldname'] = array(<br />   '#type' =&gt; 'text_format',<br />   '#title' =&gt; t('WYSIWYG Text field'),<br />   '#format' =&gt; 'full_html' //the default format used for editor.<br /> );</code></p> <h2>Type Container</h2> <p>A 'container' allow to hold  fields, markup ... or to group of fields with all advantages of the api field element.<br /> Example : Add the states visible to a type markup.<br /><code>$form['the_container'] = [<br />   '#type' =&gt; "container",<br />   '#prefix' =&gt; "&lt;div class='text'&gt;",<br />   '#suffix' =&gt; "&lt;/div&gt;",<br />   '#states' =&gt; ['visible' =&gt; ['select[name="type"]' =&gt; ['value' =&gt; '1'],],],<br /> ];<br /> $form['the_container']['markup'] = [<br />   '#markup' =&gt; "A text",<br /> ];</code></p> <h2>Number field</h2> <p>Example of number field on drupal 8 :<br /><code>$form['count'] = [<br />   '#type' =&gt; 'number',<br />   '#title' =&gt; 'A Number',<br />   '#step' =&gt; 2,<br />   '#min' =&gt; 1,<br />   '#max' =&gt; 10,<br />   '#required' =&gt; TRUE,<br />   '#default_value' =&gt; 4,<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/drupal-7" hreflang="en">Drupal 7</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/form" hreflang="en">Form</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/103/create-a-form-drupal-8-using-form-api" hreflang="en">Create a Form with Drupal 8 using Form API</a></div> <div class="field__item"><a href="/en/tutoriels/90/build-a-from-from-another-form" hreflang="en">Build a From from another Form</a></div> <div class="field__item"><a href="/en/tutoriels/105/form-redirection-on-submit" hreflang="en">Form redirection on submit</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=225&amp;2=comment&amp;3=comment" token="HPDFzG4-h-wX24DjfMFof0PqJ5pxbAaeYilZ-jFAA_g"></drupal-render-placeholder> </section> Fri, 26 Aug 2016 12:01:30 +0000 editor 225 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/225/form-api-of-drupal-8#comments Get Entity, Node, Taxonomy from URL Path http://drupal8.ovh/en/tutoriels/224/get-entity-node-taxonomy-from-url-path <span class="field field--name-title field--type-string field--label-hidden">Get Entity, Node, Taxonomy from URL Path</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 element bu url ?</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, 08/24/2016 - 11:42</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h3>Drupal 8</h3> <p> </p> <h3>Drupal 7</h3> <p><strong>Get the node</strong><br /><code>$node = menu_get_object();</code></p> <p><strong>Get Path alias</strong><br /><code>$alias = drupal_get_path_alias();</code></p> <p><strong>Get path alias</strong><br /><code>$alias = drupal_get_path_alias();<br /> $path = drupal_lookup_path("source", $alias);</code></p> <p><strong>Get node from path alias</strong><br /><code>$alias = drupal_get_path_alias();<br /> $path = drupal_lookup_path("source", $alias);<br /> $node = menu_get_object("node", 1, $path);</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/drupal-7" hreflang="en">Drupal 7</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=224&amp;2=comment&amp;3=comment" token="itnOa09JDG9A7RDQGnY1h1Jh5Ab9KPQL1OekFVgpadc"></drupal-render-placeholder> </section> Wed, 24 Aug 2016 09:42:08 +0000 editor 224 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/224/get-entity-node-taxonomy-from-url-path#comments Change Drupal 8 text field maximum length http://drupal8.ovh/en/tutoriels/223/change-drupal-8-text-field-maximum-length <span class="field field--name-title field--type-string field--label-hidden">Change Drupal 8 text field maximum length</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"><h2>How to Modify Drupal 8/ Drupal 7 text field maximum length ?</h2> </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, 08/22/2016 - 16:20</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>If there no data, You can use <span>field_update_field (tested on D7) Like:</span></p> <p><code>$field = array(<br /> 'field_name' =&gt; 'field_name',<br /> 'type' =&gt; 'text_long',<br /> 'cardinality' =&gt; 1,<br /> 'settings' =&gt; array('max_length' =&gt; 0),<br /> );<br /> field_update_field($field);</code></p> <p><span>But with data, you must change the database.</span></p> <h2><span>Change the size of a field (With data)</span></h2> <p>The 3 things that need to do:</p> <ol><li>Change the VARCHAR length of the value column in the field_data_{fieldname} table</li> <li>Change the VARCHAR length of the value column in the field_revision_{fieldname} table</li> <li>Update the configuration of the field to reflect the new max length setting</li> </ol><p> </p> <p>This methode work on Drupal 7 and Drupal 8.</p> <p>Example:</p> <p><code>function mymodule_change_text_field_max_length ($field_name, $new_length) {<br />   $field_name = 'fieldname';<br />   // Get the current settings<br />   $result = db_query('SELECT data FROM {field_config} WHERE field_name = :name', array(':name' =&gt; $field_name))-&gt;fetchField();<br />   // Change the settings<br />   $data = unserialize($result);<br />   $may_length = 20000;<br />   if ($new_length &gt; $may_length) {<br />     $new_length = $may_length;<br />   }<br />   $data['settings']['max_length'] = $new_length;<br />   // Write settings back to the database.<br />   db_update('field_config')<br />     -&gt;fields(array( 'data' =&gt; serialize($data), 'type' =&gt; "text_long"))<br />     -&gt;condition('field_name', $field_name)</code><br /><code>    -&gt;execute();<br />   // Update the value column in both the _data and _revision tables for the field<br />   $new_field = array('type' =&gt; 'varchar', 'length' =&gt; $new_length);<br />   $col_name = $field_name . '_value';<br />   db_change_field("field_data_$field_name", $col_name, $col_name, $new_field);<br />   db_change_field("field_revision_$field_name", $col_name, $col_name, $new_field);<br />   // Flush the caches<br />   drupal_flush_all_caches();<br /> }<br /> function mymodule_update_8002() {<br />   mymodule_change_text_field_max_length('field_name', 1000);<br /> }</code></p> <p> </p> <p>Another Methode :</p> <p><code>function mymodule_change_text_field_max_length ($field_name, $new_length) {<br />   $field_table = 'field_data_' . $field_name;<br />   $field_revision_table = 'field_revision_' . $field_name;<br />   $field_column = $field_name . '_value';<br />   $may_length = 20000;<br />   if($new_length&gt;$may_length){<br />     $new_length = $may_length;<br />   }</code></p> <p><code>  // Alter value field length in fields table<br />   db_query("ALTER TABLE `{$field_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");<br />   // Alter value field length in fields revision table<br />   db_query("ALTER TABLE `{$field_revision_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");</code></p> <p><code>//Update field type if need<br /> //<span>db_query</span><span>(</span><span>"UPDATE {field_config} SET type = 'text_long' WHERE field_name = '</span></code> <code>$field_name</code> <code><span>'"</span><span>);</span></code></p> <p><code>  // Update field config with new max length<br />   $result = db_query("SELECT CAST(`data` AS CHAR($may_length) CHARACTER SET utf8) FROM `field_config` WHERE field_name = '{$field_name}'");<br />   $config = $result-&gt;fetchField();<br />   $config_array = unserialize($config);<br />   $config_array['settings']['max_length'] = $new_length;<br />   $config = serialize($config_array);<br />   db_update('field_config')<br />     -&gt;fields(array('data' =&gt; $config))<br />     -&gt;condition('field_name', $field_name)<br />     -&gt;execute();<br /> }<br /> function mymodule_update_8002() {<br />   mymodule_change_text_field_max_length('field_name', 1000);<br /> }</code></p> <p>TODO : Update Field <strong>widget </strong>to <strong>text_textarea</strong><br /><code>//'widget' =&gt; array('type' =&gt; 'text_textarea')</code></p> <p> </p> <p>Another Example : http://www.pixelite.co.nz/article/convert-existing-textfield-textarea-drupal-7/</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/database" hreflang="en">Database</a></li> </ul> </div> <div class="field field--name-field-related-contents field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/en/tutoriels/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">Comments</h2> <a id="comment-69"></a> <article role="article" data-comment-user-id="0" about="/en/comment/69" typeof="schema:Comment" class="comment js-comment by-anonymous clearfix"> <span class="hidden" data-comment-timestamp="1472479596"></span> <footer class="comment__meta"> <article typeof="schema:Person" about="/user/0" class="profile"> </article> <p class="comment__author"><span rel="schema:author"><span lang="" typeof="schema:Person" property="schema:name" datatype="">Federico (not verified)</span></span> </p> <p class="comment__time">Mon, 08/29/2016 - 16:06 <span property="schema:dateCreated" content="2016-08-29T14:06:36+00:00" class="rdf-meta hidden"></span> </p> <p class="comment__permalink"><a href="/en/comment/69#comment-69" hreflang="en">Permalink</a></p> </footer> <div class="comment__content"> <h3 property="schema:name" datatype=""><a href="/en/comment/69#comment-69" class="permalink" rel="bookmark" hreflang="en">Hi! Where is this code…</a></h3> <div property="schema:text" class="clearfix text-formatted field field--name-comment-body field--type-text-long field--label-hidden field__item"><p>Hi! Where is this code supposed to be used? Thanks</p> </div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=69&amp;1=default&amp;2=en&amp;3=" token="o1zFYy9KxIR8Stf0aEbqpiHwVXE8TEr8fe8zsdrWLhw"></drupal-render-placeholder> </div> </article> <div class="indented"><a id="comment-71"></a> <article role="article" data-comment-user-id="2" about="/en/comment/71" typeof="schema:Comment" class="comment js-comment by-node-author clearfix"> <span class="hidden" data-comment-timestamp="1472544564"></span> <footer class="comment__meta"> <article typeof="schema:Person" about="/index.php/en/users/editor" class="profile"> </article> <p class="comment__author"><span rel="schema:author"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> </p> <p class="comment__time">Tue, 08/30/2016 - 10:09 <span property="schema:dateCreated" content="2016-08-30T08:09:24+00:00" class="rdf-meta hidden"></span> </p> <p class="comment__permalink"><a href="/en/comment/71#comment-71" hreflang="en">Permalink</a></p> <p class="visually-hidden">In reply to <a href="/en/comment/69#comment-69" class="permalink" rel="bookmark" hreflang="en">Hi! Where is this code…</a> by <span lang="" typeof="schema:Person" property="schema:name" datatype="">Federico (not verified)</span></p> </footer> <div class="comment__content"> <h3 property="schema:name" datatype=""><a href="/en/comment/71#comment-71" class="permalink" rel="bookmark" hreflang="en">Create a custom module</a></h3> <div property="schema:text" class="clearfix text-formatted field field--name-comment-body field--type-text-long field--label-hidden field__item"><p>Hi,</p> <p>For that you must create a custom module like : <a href="/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page" rel="nofollow">/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page</a><br /> then use the file yourmodule.module</p> </div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=71&amp;1=default&amp;2=en&amp;3=" token="jjsJX07feELSKNXNoZfS8lQVcmZMiRw8hkIdPAlcm-k"></drupal-render-placeholder> </div> </article> </div><a id="comment-117"></a> <article role="article" data-comment-user-id="0" about="/en/comment/117" typeof="schema:Comment" class="comment js-comment by-anonymous clearfix"> <span class="hidden" data-comment-timestamp="1485180477"></span> <footer class="comment__meta"> <article typeof="schema:Person" about="/user/0" class="profile"> </article> <p class="comment__author"><span rel="schema:author"><span lang="" typeof="schema:Person" property="schema:name" datatype="">Val (not verified)</span></span> </p> <p class="comment__time">Mon, 01/23/2017 - 15:07 <span property="schema:dateCreated" content="2017-01-23T14:07:57+00:00" class="rdf-meta hidden"></span> </p> <p class="comment__permalink"><a href="/en/comment/117#comment-117" hreflang="en">Permalink</a></p> </footer> <div class="comment__content"> <h3 property="schema:name" datatype=""><a href="/en/comment/117#comment-117" class="permalink" rel="bookmark" hreflang="en">Undefined table: 7 ERROR: …</a></h3> <div property="schema:text" class="clearfix text-formatted field field--name-comment-body field--type-text-long field--label-hidden field__item"><p>Undefined table: 7 ERROR:  relation "field_config" does not exist </p> </div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=117&amp;1=default&amp;2=en&amp;3=" token="mVk4iwmCmBiWd6anljP4eYUloEn1z-rgXkCDZ-65j7s"></drupal-render-placeholder> </div> </article> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=223&amp;2=comment&amp;3=comment" token="lGhW9joBHVl6CKXSYXy401cYFK1c8SlyV_6tfTAIVbY"></drupal-render-placeholder> </section> Mon, 22 Aug 2016 14:20:17 +0000 editor 223 at http://drupal8.ovh http://drupal8.ovh/en/tutoriels/223/change-drupal-8-text-field-maximum-length#comments