Hook http://lobsterr.me/ en How to hide a local task (tab) in Drupal? http://lobsterr.me/post/how-hide-local-task-tab-drupal <span class="field field--name-title field--type-string field--label-hidden">How to hide a local task (tab) in Drupal?</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">09/13/2019</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><p>Quite often we want to have a parent tab to be selected, but the sublevel tasks are not displayed or we just want remove completely some tabs for user of specific roles. </p> <p>There are two ways to do it:</p> <p>The first option is to use <strong>hook_menu_local_tasks_alter</strong></p> <pre> <code class="language-php">function your_module_local_tasks_alter(&amp;$local_tasks) { unset($local_tasks['id_of_local_tasks']); }</code></pre> <p>This code will remove your tab completely from the system, it means it will not be displayed anywhere, but sometimes we want to hide the tab only on the specific pages</p> <p>The second option is to use <strong>hook_menu_local_tasks_alter</strong></p> <pre> <code class="language-php">function your_module_menu_local_tasks_alter(&amp;$data, $route_name) { $routes = ['entity.node.canonical']; // add your routes to this array and your tab will be hidden for this routes if (in_array($route_name, $routes)) { unset($data['tabs'][0]['id_of_local_task']); } }</code></pre> <p> </p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/menu" hreflang="en">Menu</a></div> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> <div class="field__item btn btn-secondary"><a href="/tags/hook" hreflang="en">Hook</a></div> </div> <div class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush"> <div class="field__label">Related items</div> <ul class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush field__items"> <li class="field__item list-group-item"><a href="/post/how-add-local-task-tab-drupal" hreflang="en">How to add a local task (tab) in Drupal?</a></li> <li class="field__item list-group-item"><a href="/post/how-add-local-task-custom-page-drupal" hreflang="en">How to add a local task to custom page in Drupal?</a></li> </ul> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Fri, 13 Sep 2019 09:25:22 +0000 LOBsTerr 14 at http://lobsterr.me How to alter options for dropdown (select input) in Drupal http://lobsterr.me/post/how-alter-options-dropdown-select-input-drupal <span class="field field--name-title field--type-string field--label-hidden">How to alter options for dropdown (select input) in Drupal</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">09/04/2019</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><p>Sometimes we need to alter or restrict some options for dropdown of entity reference field. One of the solution to use views and provide views entity reference widget, but in some cases the logic would be too complex and we will have to implement custom plugins for views. The second solution could be just replace options for dropdown widget. For this we need implement "hook_field_widget_form_alter"</p> <pre> <code class="language-php">/** * Implements hook_field_widget_form_alter(). */ function your_module_field_widget_form_alter( if (!empty($field_definition) &amp;&amp; $field_definition-&gt;getName() == 'fut_collection') { $element['#options'] = _your_module_get_input_options(); } } /** * Gets group collection options. */ function _your_module_get_input_options() { $options = []; // here your custom logic to get ids. $items = Term::loadMultiple($ids); if ($items ) { // a default empty value. $options['_none'] = t('- None -'); foreach ($items as $item) { $options[$item-&gt;id()] = $item-&gt;getName(); } } return $options; }</code></pre> <p>Keep in mind that you have to use the entity ids of entity type for your entity reference field. Also, if you want to provide the default value without any value, you should use this code:</p> <pre> <code class="language-php">$options['_none'] = t('- None -');</code></pre> <p>and not like</p> <pre> <code class="language-php">$options[''] = t('- None -');</code></pre> <p>In other case you can face some unexpected bugs</p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> <div class="field__item btn btn-secondary"><a href="/tags/form-api" hreflang="en">Form API</a></div> <div class="field__item btn btn-secondary"><a href="/tags/hook" hreflang="en">Hook</a></div> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Wed, 04 Sep 2019 09:05:50 +0000 LOBsTerr 10 at http://lobsterr.me