Dynamic menu links for adding actors

This commit is contained in:
Naomi 2018-10-24 17:40:26 +01:00
parent fd5d145866
commit 10b99f132e
3 changed files with 68 additions and 0 deletions

View File

@ -34,3 +34,7 @@ opencase.send_feedback:
menu_name: openc
url: internal:/contact
weight: 1
opencase.add_actors_links:
class: Drupal\opencase\Plugin\Menu\AddActorsMenuLink
deriver: Drupal\opencase\Plugin\Derivative\AddActorsMenuLink
menu_name: opencase-add-actors

View File

@ -0,0 +1,55 @@
<?php
namespace Drupal\opencase\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
 * Derivative class that provides the menu links adding various types of actors
 */
class AddActorsMenuLink extends DeriverBase implements ContainerDeriverInterface {
/**
   * @var EntityTypeManagerInterface $entityTypeManager.
   */
protected $entityTypeManager;
/**
   * Creates a AddActorsMenuLink instance.
   *
   * @param $base_plugin_id
   * @param EntityTypeManagerInterface $entity_type_manager
   */
public function __construct($base_plugin_id, EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
   * {@inheritdoc}
   */
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$base_plugin_id,
$container->get('entity_type.manager')
);
}
/**
   * {@inheritdoc}
   */
public function getDerivativeDefinitions($base_plugin_definition) {
$links = [];
$actorTypes = $this->entityTypeManager->getStorage('oc_actor_type')->loadMultiple();
foreach ($actorTypes as $id => $actorType) {
$links[$id] = [
'title' => "Add a ". $actorType->label(),
'route_name' => "entity.oc_actor.add_form",
'route_parameters' => ['oc_actor_type' => $actorType->id()]
] + $base_plugin_definition;
}
return $links;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace Drupal\opencase\Plugin\Menu;
use Drupal\Core\Menu\MenuLinkDefault;
/**
* Represents a menu link for adding an actor of some type.
*/
class AddActorsMenuLink extends MenuLinkDefault {}