BlockPluginUI.php 7.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php

/**
 * Contains \Drupal\block\Plugin\system\plugin_ui\BlockPluginUI.
 */

namespace Drupal\block\Plugin\system\plugin_ui;

use Drupal\system\Plugin\PluginUIBase;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

/**
 * Defines an overrideable UI for block selection, configuration, and placement.
 *
 * @Plugin(
 *   id = "block_plugin_ui",
 *   module = "block",
 *   all_plugins = @Translation("All Blocks"),
20
 *   config_path = "admin/structure/block/add",
21 22 23 24 25 26 27 28 29 30 31 32
 *   default_task = TRUE,
 *   derivative = "Drupal\block\Plugin\Derivative\BlockPluginUI",
 *   facets = {
 *     "module" = @Translation("Modules")
 *   },
 *   link_title = @Translation("Configure block"),
 *   manager = "Drupal\block\Plugin\Type\BlockManager",
 *   menu = TRUE,
 *   path = "admin/structure/block/list",
 *   suffix = "add",
 *   task_suffix = "library",
 *   task_title = @Translation("Library"),
33
 *   title = @Translation("Add block"),
34 35 36 37 38 39 40 41 42 43 44 45 46
 *   title_attribute = "subject",
 *   type = MENU_LOCAL_ACTION
 * )
 */
class BlockPluginUI extends PluginUIBase {

  /**
   * Overrides \Drupal\system\Plugin\PluginUIBase::form().
   *
   * @todo Add inline documentation to this method.
   */
  public function form($form, &$form_state, $facet = NULL) {
    // @todo Add an inline comment here.
47
    list($plugin, $theme) = explode(':', $this->getPluginId());
48 49 50 51
    $plugin_definition = $this->getDefinition();
    $manager = new $plugin_definition['manager']();
    $plugins = $manager->getDefinitions();
    $form['#theme'] = 'system_plugin_ui_form';
52 53 54 55 56 57 58 59
    $form['theme'] = array(
      '#type' => 'value',
      '#value' => $theme,
    );
    $form['manager'] = array(
      '#type' => 'value',
      '#value' => $manager,
    );
60 61 62 63 64 65 66 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
    $form['instance'] = array(
      '#type' => 'value',
      '#value' => $this,
    );
    $form['right']['block'] = array(
      '#type' => 'textfield',
      '#title' => t('Search'),
      '#autocomplete_path' => 'system/autocomplete/' . $this->getPluginId(),
    );
    $form['right']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Next'),
    );
    $rows = array();
    foreach ($plugins as $plugin_id => $display_plugin_definition) {
      if (empty($facet) || $this->facetCompare($facet, $display_plugin_definition)) {
        $rows[] = $this->row($plugin_id, $display_plugin_definition);
      }
      foreach ($plugin_definition['facets'] as $key => $title) {
        $facets[$key][$display_plugin_definition[$key]] = $this->facetLink($key, $plugin_id, $display_plugin_definition);
      }
      $form['right']['all_plugins'] = array(
        '#type' => 'link',
        '#title' => $plugin_definition['all_plugins'],
        '#href' => $this->allPluginsUrl($plugin_id, $display_plugin_definition),
      );
      foreach ($facets as $group => $values) {
        $form['right'][$group] = array(
          '#theme' => 'links',
          '#heading' => array(
            'text' => $plugin_definition['facets'][$group],
            'level' => 'h3',
          ),
          '#links' => $values,
        );
      }
      $form['left']['plugin_library'] = array(
        '#theme' => 'table',
        '#header' => $this->tableHeader(),
        '#rows' => $rows,
      );
    }
    return $form;
  }

105 106 107 108 109 110 111 112 113 114
  /**
   * Overrides \Drupal\system\Plugin\PluginUIBase::formValidate().
   */
  public function formValidate($form, &$form_state) {
    $definitions = $form_state['values']['manager']->getDefinitions();
    if (!isset($definitions[$form_state['values']['block']])) {
      form_set_error('block', t('You must select a valid block.'));
    }
  }

115 116 117 118
  /**
   * Overrides \Drupal\system\Plugin\PluginUIBase::formSubmit().
   */
  public function formSubmit($form, &$form_state) {
119
    $form_state['redirect'] = 'admin/structure/block/add/' . $form_state['values']['block'] . '/' . $form_state['values']['theme'];
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 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  }

  /**
   * Overrides \Drupal\system\Plugin\PluginUIBase::access().
   */
  public function access() {
    list($plugin, $theme) = explode(':', $this->getPluginId());
    return _block_themes_access($theme);
  }

  /**
   * Overrides \Drupal\system\Plugin\PluginUIBase::tableHeader().
   */
  public function tableHeader() {
    return array(t('Subject'), t('Operations'));
  }

  /**
   * Overrides \Drupal\system\Plugin\PluginUIBase::row().
   */
  public function row($display_plugin_id, array $display_plugin_definition) {
    $plugin_definition = $this->getDefinition();
    list($plugin, $theme) = explode(':', $this->getPluginId());
    $row = array();
    $row[] = $display_plugin_definition['subject'];
    $row[] = array('data' => array(
      '#type' => 'operations',
      '#links' => array(
        'configure' => array(
          'title' => $plugin_definition['link_title'],
          'href' => $plugin_definition['config_path'] . '/' . $display_plugin_id . '/' . $theme,
        ),
      ),
    ));
    return $row;
  }

  /**
   * Creates a facet link for a given facet of a display plugin.
   *
   * Provides individually formatted links for the faceting that happens within
   * the user interface. Since this is a faceting style procedure, each plugin
   * may be parsed multiple times in order to extract all facets and their
   * appropriate labels.
   *
   * The $display_plugin_id and $display_plugin_definition are provided for
   * convenience when overriding this method.
   *
   * @param string $facet
   *   A simple string indicating what element of the $display_plugin_definition
   *   to utilize for faceting.
   * @param string $display_plugin_id
   *   The plugin ID of the plugin we are currently parsing a facet link from.
   * @param array $display_plugin_definition
   *   The plugin definition we are parsing.
   *
   * @return array
   *   Returns a row array comaptible with theme_links().
   */
  protected function facetLink($facet, $display_plugin_id, array $display_plugin_definition) {
    $plugin_definition = $this->getDefinition();
    return array(
      'title' => $display_plugin_definition[$facet],
      'href' => $plugin_definition['path'] . '/' . $this->getPluginId() . '/' . $facet . ':' . $display_plugin_definition[$facet],
    );
  }

  /**
   * Determines whether a given facet should be displayed for a plugin.
   *
   * Compares a given plugin definition with the selected facet to determine if
   * the plugin should be displayed in the user interface.
   *
   * @param string $facet
   *   A colon separated string representing the key/value paring of a selected
   *   facet.
   * @param array $display_plugin_definition
   *   The plugin definition to be compared.
   *
   * @return bool
   *   Returns TRUE if the selected facet matches this plugin.
   */
  protected function facetCompare($facet, $display_plugin_definition) {
    list($facet_type, $option) = explode(':', $facet);
    return $option == $display_plugin_definition[$facet_type];
  }

  /**
   * Provides an "all" style link to reset the facets.
   *
   * The $display_plugin_id and $display_plugin_definition are provided for
   * convenience when overriding this method.
   *
   * @param string $display_plugin_id
   *   The plugin ID of the plugin we are currently parsing a facet link from.
   * @param array $display_plugin_definition
   *   The plugin definition we are parsing.
   *
   * @return string
   *   Returns a simple URL string for use within l().
   */
  protected function allPluginsUrl($display_plugin_id, $display_plugin_definition) {
    $plugin_definition = $this->getDefinition();
    return $plugin_definition['path'] . '/' . $this->getPluginId() . '/add';
  }

}