0

I am trying to add an extra text field to the menu entity. My field is showing up and saving input but the input saves for all menus not just the one I entered the data for.

This is how it is now:

function myModule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'menu_edit_form') {
    $config = \Drupal::service('config.factory')->getEditable('myModule.settings');
    $my_field = $config->get('my_field');
    $form['my_field'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $my_field,
    '#weight' => '0',
    '#required' => FALSE,
    );

    $form['actions']['submit']['#submit'][] = 'myModule_menu_edit_submit';
  }
}

function myModule_menu_edit_submit($form, $form_state) {
  $config = \Drupal::service('config.factory')->getEditable('myModule.settings');
  $config->set('my_field', $form_state->getValue('my_field'))->save();
}

I need to save different data for each form, so I was trying to do change the submit function to:

function myModule_menu_edit_submit($form, $form_state) {
  $key = $form_state->getformObject()->getEntity()->id();
  $config = \Drupal::service('config.factory')->getEditable('myModule.settings');
  $config->set($key, $form_state->getValue('my_field'))->save();
}

If I do this though, when I enter the data in the field and save, it says updated successfully but the field is blank. Not sure what I'm doing wrong.

7
  • 1
    There is a module for that(TM) - drupal.org/project/menu_item_extras
    – Hudri
    Jul 13, 2020 at 22:15
  • Did you install the module or re-install if already installed? By the way, using the field is not so easy because the menu link entity is behind a menu link plugin which has methods only for the standard fields. See drupal.stackexchange.com/questions/235754/…
    – 4uk4
    Jul 14, 2020 at 7:04
  • @Hudri So I want to add the field to the actual menu where you add/edit the actual menu, not the menu links. Does that module work for my situation as well? @4k4 I haven't tried reinstalling the module. Do you think making a hook_update_N and putting my code in that and then doing a drush updb would work?
    – Jordan
    Jul 14, 2020 at 14:40
  • The module is for menu links (this individual items below a menu). The menu itself is not a fieldable content entity, you can't add content fields there. A menu is a configuration entity, you only can add 3rd party config settings there.
    – Hudri
    Jul 14, 2020 at 15:22
  • Example for menu 3rd party setting: git.drupalcode.org/project/menu_trail_by_path/-/blob/8.x-1.x/…
    – Hudri
    Jul 14, 2020 at 15:25

1 Answer 1

1

I figured out how to add a field to the menus (not menu links, the actual menus). I use the machine name of the menu's title as a key and store the value with the key I use in the $form variable.

Here is my code:

function myModule_form_menu_edit_form_alter(&$form, $form_state, $form_id) {
  $config = \Drupal::service('config.factory')->getEditable('myModule.settings');
  $my_field = $config->get(str_replace('-', '_', $form_state->getformObject()->getEntity()->id()));

  $form['my_field'] = array(
  '#type' => 'textfield',
  '#title' => t('Title'),
  '#default_value' => $my_field,
  '#weight' => '0',
  '#required' => FALSE,
  );

  $form['actions']['submit']['#submit'][] = 'myModule_custom_submit';
}

function myModule_custom_submit($form, $form_state) {
  $key = str_replace('-', '_', $form_state->getformObject()->getEntity()->id());
  $config = \Drupal::service('config.factory')->getEditable('myModule.settings');
  $config->set($key, $form_state->getValue('my_field'))->save();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.