Commit f6932126 authored by catch's avatar catch
Browse files

Issue #2684577 by mohit_aghera, tim-diels, pooja saraah, gapple, smustgrave,...

Issue #2684577 by mohit_aghera, tim-diels, pooja saraah, gapple, smustgrave, quietone: Entity operations for menu links are hardcoded in edit menu form
parent f7c10393
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -150,6 +150,13 @@ public function getDeleteRoute() {
   * {@inheritdoc}
   */
  public function getEditRoute() {
    return Url::fromRoute('menu_ui.link_edit', ['menu_link_plugin' => $this->getPluginId()]);
  }

  /**
   * {@inheritdoc}
   */
  public function getResetRoute(): Url|NULL {
    return NULL;
  }

@@ -160,6 +167,41 @@ public function getTranslateRoute() {
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getOperations(): array {
    $operations = [];

    $operations['edit'] = [
      'title' => $this->t('Edit'),
      'url' => $this->getEditRoute(),
    ];

    // Links can either be reset or deleted, not both.
    if ($this->isResettable()) {
      $operations['reset'] = [
        'title' => $this->t('Reset'),
        'url' => $this->getResetRoute(),
      ];
    }
    elseif ($this->isDeletable()) {
      $operations['delete'] = [
        'title' => $this->t('Delete'),
        'url' => $this->getDeleteRoute(),
      ];
    }

    if ($this->isTranslatable()) {
      $operations['translate'] = [
        'title' => $this->t('Translate'),
        'url' => $this->getTranslateRoute(),
      ];
    }

    return $operations;
  }

  /**
   * {@inheritdoc}
   */
+8 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\Core\Menu;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -80,6 +81,13 @@ public function isResettable() {
    return (bool) $this->staticOverride->loadOverride($this->getPluginId());
  }

  /**
   * {@inheritdoc}
   */
  public function getResetRoute(): Url {
    return Url::fromRoute('menu_ui.link_reset', ['menu_link_plugin' => $this->getPluginId()]);
  }

  /**
   * {@inheritdoc}
   */
+22 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Url;

/**
 * Defines an interface for classes providing a type of menu link.
@@ -219,6 +220,15 @@ public function getDeleteRoute();
   */
  public function getEditRoute();

  /**
   * Returns route information for a route to reset the menu link.
   *
   * @return \Drupal\Core\Url|null
   *   A Url object, or NULL if there is no route (e.g. when the link is not
   *   resettable).
   */
  public function getResetRoute(): Url|NULL;

  /**
   * Returns route information for a route to translate the menu link.
   *
@@ -228,4 +238,16 @@ public function getEditRoute();
   */
  public function getTranslateRoute();

  /**
   * Provides an array of information to build a list of operation links.
   *
   * @return array
   *   An associative array of operation link data for this menu link, keyed by
   *   operation name, containing the following key-value pairs:
   *   - title: The localized title of the operation.
   *   - url: An instance of \Drupal\Core\Url for the operation URL.
   *   - weight: The weight of this operation.
   */
  public function getOperations(): array;

}
+2 −1
Original line number Diff line number Diff line
@@ -32,7 +32,8 @@
 *     "form" = {
 *       "default" = "Drupal\menu_link_content\Form\MenuLinkContentForm",
 *       "delete" = "Drupal\menu_link_content\Form\MenuLinkContentDeleteForm"
 *     }
 *     },
 *     "list_builder" = "Drupal\menu_link_content\MenuLinkListBuilder"
 *   },
 *   admin_permission = "administer menu",
 *   base_table = "menu_link_content",
+72 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\menu_link_content;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a menu link list builder.
 */
class MenuLinkListBuilder extends EntityListBuilder {

  /**
   * The redirect destination.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface
   */
  protected $redirectDestination;

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('entity_type.manager')->getStorage($entity_type->id()),
      $container->get('redirect.destination')
    );
  }

  /**
   * Constructs a new instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
   *   The redirect destination.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, RedirectDestinationInterface $redirect_destination) {
    parent::__construct($entity_type, $storage);

    $this->redirectDestination = $redirect_destination;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity) {
    $operations = parent::getDefaultOperations($entity);

    $destination = $this->redirectDestination->get();
    foreach ($operations as $key => $operation) {
      $operations[$key]['query']['destination'] = $destination;
    }

    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    throw new \LogicException('This list builder can only provide operations. It does not build lists.');
  }

}
Loading