ActionListController.php 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Contains \Drupal\action\ActionListController.
 */

namespace Drupal\action;

use Drupal\Component\Utility\String;
use Drupal\Core\Action\ActionManager;
use Drupal\Core\Entity\EntityControllerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityListController;
use Drupal\Core\Entity\EntityStorageControllerInterface;
16
use \Drupal\Core\Extension\ModuleHandlerInterface;
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
use Drupal\action\Form\ActionAdminManageForm;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a listing of Actions.
 */
class ActionListController extends ConfigEntityListController implements EntityControllerInterface {

  /**
   * @var bool
   */
  protected $hasConfigurableActions = FALSE;

  /**
   * The action plugin manager.
   *
   * @var \Drupal\Core\Action\ActionManager
   */
  protected $actionManager;

  /**
   * Constructs a new ActionListController object.
   *
   * @param string $entity_type
   *   The entity type.
42 43
   * @param array $entity_info
   *   An array of entity info for the entity type.
44 45 46 47
   * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage
   *   The action storage controller.
   * @param \Drupal\Core\Action\ActionManager $action_manager
   *   The action plugin manager.
48 49
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke hooks on.
50
   */
51 52
  public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ActionManager $action_manager, ModuleHandlerInterface $module_handler) {
    parent::__construct($entity_type, $entity_info, $storage, $module_handler);
53 54 55 56 57 58 59 60 61 62

    $this->actionManager = $action_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
    return new static(
      $entity_type,
63
      $entity_info,
64
      $container->get('plugin.manager.entity')->getStorageController($entity_type),
65 66
      $container->get('plugin.manager.action'),
      $container->get('module_handler')
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    );
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    $entities = parent::load();
    foreach ($entities as $entity) {
      if ($entity->isConfigurable()) {
        $this->hasConfigurableActions = TRUE;
        continue;
      }
    }
    return $entities;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $row['type'] = $entity->getType();
    $row['label'] = String::checkPlain($entity->label());
    if ($this->hasConfigurableActions) {
      $row['operations']['data'] = $this->buildOperations($entity);
    }
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header = array(
      'type' => t('Action type'),
      'label' => t('Label'),
      'operations' => t('Operations'),
    );
    return $header;
  }

  /**
   * {@inheritdoc}
   */
  public function getOperations(EntityInterface $entity) {
    $operations = array();
    if ($entity->isConfigurable()) {
      $uri = $entity->uri();
      $operations['edit'] = array(
        'title' => t('Configure'),
        'href' => $uri['path'],
        'options' => $uri['options'],
        'weight' => 10,
      );
      $operations['delete'] = array(
        'title' => t('Delete'),
        'href' => $uri['path'] . '/delete',
        'options' => $uri['options'],
        'weight' => 100,
      );
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build['action_header']['#markup'] = '<h3>' . t('Available actions:') . '</h3>';
    $build['action_table'] = parent::render();
    if (!$this->hasConfigurableActions) {
      unset($build['action_table']['#header']['operations']);
    }
    $build['action_admin_manage_form'] = drupal_get_form(new ActionAdminManageForm($this->actionManager));
    return $build;
  }

}