I am creating a custom theme for Drupal 8. I would like to have it so when the theme is installed it adds some custom text to the admin add link page for only the main menu.

For example:

The page url is /admin/structure/menu/manage/main/add?destination=/admin/structure/menu

under the link field it reads:

  • The location this menu link points to.
  • Start typing the title of a piece of content to select it. You can also enter an internal path such as /node/add or an external URL such as http://example.com. Enter <front> to link to the front page.

I would like it to read:

  • The location this menu link points to.
  • Start typing the title of a piece of content to select it. You can also enter an internal path such as /node/add or an external URL such as http://example.com. Enter <front> to link to the front page.
  • Enter custom text

How can I do this, would it be a hook or what?

Comments

mcrittenden’s picture

You just want to add some text to that form? If so, you're looking for hook_form_alter() - https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/8.7.x

If, however, you're wanting to add a new field, then you probably want https://www.drupal.org/project/menu_item_extras

- Mike, from Little Blue Labs and Drupal Check

lzande’s picture

I have the following in my .theme file.

<?php
/**
 * @file
 * Theme functions for reach new heights.
 */

use \Drupal\Core\Form;

/**
 * Implements hook_form_alter()
 */
echo "TEST";

function reach_new_heights_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
var_dump($form_id);
 }

The TEST string only shows up when i visit the settings page for the theme and not on all pages.
If I go to a page with a form on it, I don't see the var_dump. any ideas?

Sorry if this sounds like a simple question. I have been working with Drupal for 6 years but never had to do hooks or any module development.

EDIT: I figured it out. I had to set the admin theme of my site to the one i was working on. Looks like I will need two themes for this to work. BTW: I looked at your sites Mike, you have a lot of insight in Drupal. Thanks for your help.

lzande’s picture

I have the following drupal hook in my .theme file. 

The kint before line shows the original Drupal description. The kint after line shows the modified description as TEST. However, when the form renders, the default is displayed instead of the custom one. 

Please see this image.

<?php
/**
* Implements hook_form_FORM_ID_alter().
* "The location this menu link points to."
*/
use \Drupal\Component\Render\MarkupInterface;

function reach_new_heights_admin_form_menu_link_content_menu_link_content_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

kint('before: '. $form['link']['widget']['#description']);
$form['link']['widget']['#description'] = $form['link']['widget']['#description']->create('TEST');
kint('after: '. $form['link']['widget']['#description']);

}
?>
mahtab_alam’s picture

/**
 * Implements hook_form_menu_link_content_form_alter().
 */
function my_module_form_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['link']['widget'][0]['uri']['#description']['#items'][] = t('This is my help text.');
}

Try this i was able to add the help text on links fields.
This will add one more line of help text and keep what is already present

Ridhima gupta’s picture


<?php
function mymodule_menu() {
  $items['mymodule/links'] = array(
    'title' => 'Links', 
    'page callback' => 'mymodule_links_page', 
    'access arguments' => array('access content'), 
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}
?>