ISSUE DESCRIPTION:

If you add custom field to the views menu link edit form via hook_BASE_FORM_id_alter, custom options are not saved on the form submit.

ENVIRONMENT:

OS/web server: Linux/Apache
Drupal version: 8.3.5

REPRODUCIBILITY: Always

STEPS TO REPRODUCE:

- Create a custom module (e.g. mymodule). Note that I provided compressed fully working module in the attachments.

- In the .module file add the following code in order to alter views menu link edit form, create the form submit handler and define our own views menu link class allowing options to be saved:

/**
 * Implements hook_BASE_FORM_id_alter().
 *
 * Alter menu link edit form for special menu item type (e.g. Home).
 */
function mymodule_form_menu_link_edit_alter(&$form, &$form_state, $form_id) {
  $menu_link = null;
  $menu_link_definition = $form_state->getBuildInfo()['args'][0]->getPluginDefinition();
  $options = $form_state->getBuildInfo()['args'][0]->getOptions();

  $form['mymodule_test'] = [
    '#type' => 'textfield',
    '#title' => t('MyModule test field'),
    '#default_value' => !empty($options['mymodule_test']) ? $options['mymodule_test'] : '',
  ];

  $form['#submit'][] = 'mymodule_form_menu_link_edit_submit';
}

/**
 * Callback for menu_link_edit form submit.
 */
function mymodule_form_menu_link_edit_submit(&$form, &$form_state) {
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  $menu_link_id = $form_state->getValue('menu_link_id');
  $options = ['mymodule_test' => $form_state->getValue('mymodule_test')];

  $menu_link_manager->updateDefinition($menu_link_id, ['options' => $options]);
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function mymodule_menu_links_discovered_alter(&$links) {
  foreach ($links as $key => $value) {
    if (!empty($value['provider']) && $value['provider'] == 'views') {
      if (strpos($key, 'views_view:') === 0) {
        $links[$key]['class'] = 'Drupal\mymodule\MyModuleViewsMenuLink';
      }
    }
  }
}

- Create file /src/MyModuleViewsMenuLink.php with the content below. This is the class that extends ViewsMenuLink allowing options to be saved.

<?php

namespace Drupal\mymodule;

use Drupal\views\Plugin\Menu\ViewsMenuLink;

/**
 * Defines menu links provided by views.
 */
class MyModuleViewsMenuLink extends ViewsMenuLink {

  /**
   * {@inheritdoc}
   */
  protected $overrideAllowed = [
    'menu_name' => 1,
    'parent' => 1,
    'weight' => 1,
    'expanded' => 1,
    'enabled' => 1,
    'title' => 1,
    'description' => 1,
    // MyModule: Allow override of this variable.
    'options' => 1,
  ];

}

- When we've finished coding part it's time to create a views menu link itself. Navigate to Administration > Structure > Views > Add view

- Enter "View name" and leave "View settings" default

- Check option "Create a page" under the "Page Settings" section

- Check option "Create a menu link" and select "Main navigation" in "Menu" select list. New menu item linked to the view will be displayed in the Main navigation when you visit the front page.

- Navigate to the edit form of the "Main navigation", Administration > Structure > Menus > Edit Main navigation

- Edit the view menu item by clicking the "Edit" operation. The edit form will be displayed containing the "MyModule test field" we've defined in the alter form hook.

- Enter whatever you want in the field and save the form

EXPECTED BEHAVIOR:

Entered value in the test field will be saved in the options like in any other altered menu link edit form.

ACTUAL BEHAVIOR:

Custom options are not saved at all.

NOTES:

- The similar logic works for module-defined links without any problems (e.g. Home, Login/Logout menu items...)
- The test module also contains some other extended menu links classes but they are not relevant for views menu links (or I suppose they aren't)

CommentFileSizeAuthor
mymodule.zip3.23 KBMitraX
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

MitraX created an issue. See original summary.

MitraX’s picture

Issue summary: View changes
EclipseGc’s picture

Issue tags: -Plugin system

I don't see anything that convinces me this is a plugin system level problem, so untagging it.

Super interesting use case, did you confirm your submission handler is firing? That'd be my first priority.

Eclipse

Version: 8.3.5 » 8.3.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.3.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.