AnswerBun.com

How to add a field to menus?

Drupal Answers Asked on October 26, 2021

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.

One Answer

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();
}

Answered by Jordan on October 26, 2021

Add your own answers!

Related Questions

File field upload in modal/dialog

1  Asked on December 4, 2020 by ctrnz

     

How to pre-select items in a select with chosen

1  Asked on December 3, 2020 by jav

 

Update existing node (not created by Migrate) via Migrate 8.5.x

1  Asked on December 3, 2020 by vaibhav-rana

 

Secondary Tabs and views

1  Asked on December 2, 2020 by timwhelan

 

alter Suggestion box for apache solr autosearch

2  Asked on December 1, 2020 by anamika

   

User specific permission to add links to a menu

2  Asked on November 26, 2020 by mavicc

 

How to alter sort handler to keep existing order intact in views

1  Asked on November 18, 2020 by hiranya-sarma

   

Issue using Config Split on content types

0  Asked on November 18, 2020 by vecta

   

How to create simple confirmation page?

3  Asked on November 11, 2020 by mathewdragon

 

How do I add a template suggestion for a Views block?

2  Asked on October 21, 2020 by wbeasley

       

How to programmatically manage taxonomies?

1  Asked on October 15, 2020 by deny-dias

   

CSS-tags added using Display suite on products disappears

1  Asked on October 4, 2020 by anders-wallenquist

 

I have issue with commerce_order_load_multiple function

1  Asked on October 1, 2020 by user3686276

 

Why can’t I install even if I have the required PHP version?

0  Asked on September 13, 2020 by hendrik-kaiser

 

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP