AdminController.php 6.87 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\comment\Controller\AdminController.
 */

namespace Drupal\comment\Controller;

10 11
use Drupal\comment\CommentManagerInterface;
use Drupal\field\FieldInfo;
12 13
use Drupal\Component\Utility\String;
use Drupal\Core\Controller\ControllerBase;
14
use Drupal\Core\Form\FormBuilderInterface;
15
use Drupal\field_ui\FieldUI;
16
use Symfony\Component\HttpFoundation\Request;
17 18 19 20 21
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Returns responses for comment module administrative routes.
 */
22
class AdminController extends ControllerBase {
23 24 25 26 27 28 29 30 31 32 33

  /**
   * The field info service.
   *
   * @var \Drupal\field\FieldInfo
   */
  protected $fieldInfo;

  /**
   * The comment manager service.
   *
34
   * @var \Drupal\comment\CommentManagerInterface
35 36 37
   */
  protected $commentManager;

38 39 40 41 42 43 44
  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

45 46 47 48 49 50
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('field.info'),
51 52
      $container->get('comment.manager'),
      $container->get('form_builder')
53 54 55 56 57 58 59 60
    );
  }

  /**
   * Constructs an AdminController object.
   *
   * @param \Drupal\field\FieldInfo $field_info
   *   The field info service.
61
   * @param \Drupal\comment\CommentManagerInterface $comment_manager
62
   *   The comment manager service.
63 64
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
65
   */
66
  public function __construct(FieldInfo $field_info, CommentManagerInterface $comment_manager, FormBuilderInterface $form_builder) {
67 68
    $this->fieldInfo = $field_info;
    $this->commentManager = $comment_manager;
69
    $this->formBuilder = $form_builder;
70 71 72 73 74 75 76 77 78 79 80 81 82
  }

  /**
   * Returns an overview of comment fields in use on the site.
   *
   * @return array
   *   A renderable array containing a list of comment fields, the entity
   *   type and bundle combinations on which they are in use and various
   *   operation links for configuring each field.
   */
  public function overviewBundles() {
    $header = array(
      'field_name' => $this->t('Field name'),
83 84 85 86
      'description' => array(
        'data' => $this->t('Description'),
        'class' => array(RESPONSIVE_PRIORITY_MEDIUM),
      ),
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
      'usage' => array(
        'data' => $this->t('Used in'),
        'class' => array(RESPONSIVE_PRIORITY_MEDIUM),
      ),
      'type' => $this->t('Type'),
    );

    // Add a column for field UI operations if the Field UI module is enabled.
    $field_ui_enabled = $this->moduleHandler()->moduleExists('field_ui');
    if ($field_ui_enabled) {
      $header['operations'] = $this->t('Operations');
    }

    $entity_bundles = $this->entityManager()->getAllBundleInfo();
    $entity_types = $this->entityManager()->getDefinitions();
    $rows = array();

    // Fetch a list of all comment fields.
    $fields = $this->commentManager->getAllFields();

    foreach ($fields as $entity_type => $data) {
      foreach ($data as $field_name => $field_info_map) {
        $field_info = $this->fieldInfo->getField($entity_type, $field_name);
        // Initialize the row.
        $row = array(
          'class' => $field_info->get('locked') ? array('field-disabled') : array(''),
        );

        $bundles = $field_info->getBundles();
        $sample_bundle = reset($bundles);
        $sample_instance = $this->fieldInfo->getInstance($entity_type, $sample_bundle, $field_name);

        $tokens = array(
          '@label' => $sample_instance->label,
          '@field_name' => $field_name,
        );
        $row['data']['field_name']['data'] = $field_info->get('locked') ? $this->t('@label (@field_name) (Locked)', $tokens) : $this->t('@label (@field_name)', $tokens);

125
        $row['data']['description']['data'] = $field_info->getSetting('description');
126 127 128 129 130 131 132
        $row['data']['usage']['data'] = array(
          '#theme' => 'item_list',
          '#items' => array(),
        );
        foreach ($field_info_map['bundles'] as $bundle) {
          if (isset($entity_bundles[$entity_type][$bundle])) {
            // Add the current instance.
133
            if ($field_ui_enabled && $route_info = FieldUI::getOverviewRouteInfo($entity_type, $bundle)) {
134 135 136 137 138 139 140 141
              $row['data']['usage']['data']['#items'][] = $this->l($entity_bundles[$entity_type][$bundle]['label'], $route_info['route_name'], $route_info['route_parameters']);
            }
            else {
              $row['data']['usage']['data']['#items'][] = $entity_bundles[$entity_type][$bundle]['label'];
            }
          }
        }

142
        $row['data']['type']['data'] = String::checkPlain($entity_types[$entity_type]->getLabel());
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

        if ($field_ui_enabled) {
          if ($this->currentUser()->hasPermission('administer comment fields')) {
            $links['fields'] = array(
              'title' => $this->t('Manage fields'),
              'href' => 'admin/structure/comments/manage/' . $entity_type . '__' . $field_name . '/fields',
              'weight' => 5,
            );
          }
          if ($this->currentUser()->hasPermission('administer comment display')) {
            $links['display'] = array(
              'title' => $this->t('Manage display'),
              'href' => 'admin/structure/comments/manage/' . $entity_type . '__' . $field_name . '/display',
              'weight' => 10,
            );
          }
          if ($this->currentUser()->hasPermission('administer comment form display')) {
            $links['form_display'] = array(
              'title' => $this->t('Manage form display'),
              'href' => 'admin/structure/comments/manage/' . $entity_type . '__' . $field_name . '/form-display',
              'weight' => 10,
            );
          }

          $row['data']['operations']['data'] = array(
            '#type' => 'operations',
            '#links' => $links,
          );
        }
        $rows[$entity_type . '__' . $field_name] = $row;
      }
    }

    $build['overview'] = array(
177
      '#type' => 'table',
178 179 180 181 182 183 184 185 186
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => $this->t('No comment forms available.'),
    );
    $build['#title'] = $this->t('Comment forms');

    return $build;
  }

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  /**
   * Presents an administrative comment listing.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request of the page.
   * @param string $type
   *   The type of the overview form ('approval' or 'new') default to 'new'.
   *
   * @return array
   *   Then comment multiple delete confirmation form or the comments overview
   *   administration form.
   */
  public function adminPage(Request $request, $type = 'new') {
    if ($request->request->get('operation') == 'delete' && $request->request->get('comments')) {
      return $this->formBuilder->getForm('\Drupal\comment\Form\ConfirmDeleteMultiple', $request);
    }
    else {
      return $this->formBuilder->getForm('\Drupal\comment\Form\CommentAdminOverview', $type);
    }
  }

208
}