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

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

namespace Drupal\block\Plugin\system\plugin_ui;

use Drupal\system\Plugin\PluginUIBase;
10
use Drupal\Component\Annotation\Plugin;
11
12
13
14
15
16
17
18
19
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
 *   default_task = TRUE,
 *   derivative = "Drupal\block\Plugin\Derivative\BlockPluginUI",
 *   facets = {
 *     "module" = @Translation("Modules")
 *   },
26
 *   link_title = @Translation("Place block"),
27
 *   manager = "plugin.manager.block",
28
29
30
31
32
 *   menu = TRUE,
 *   path = "admin/structure/block/list",
 *   suffix = "add",
 *   task_suffix = "library",
 *   task_title = @Translation("Library"),
33
 *   title = @Translation("Place blocks"),
34
 *   title_attribute = "admin_label",
35
36
37
38
39
40
41
42
43
44
45
46
 *   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
    $plugin_definition = $this->getDefinition();
49
50
    // @todo Find out how to let the manager be injected into the class.
    $manager = drupal_container()->get($plugin_definition['manager']);
51
52
    $plugins = $manager->getDefinitions();
    $form['#theme'] = 'system_plugin_ui_form';
53
54
55
56
57
58
59
60
    $form['theme'] = array(
      '#type' => 'value',
      '#value' => $theme,
    );
    $form['manager'] = array(
      '#type' => 'value',
      '#value' => $manager,
    );
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    $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)) {
77
        $rows[$plugin_id] = $this->row($plugin_id, $display_plugin_definition);
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
      }
      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,
        );
      }
    }
98
    // Sort rows alphabetically.
99
    asort($rows);
100
101
102
103
104
    $form['left']['plugin_library'] = array(
      '#theme' => 'table',
      '#header' => $this->tableHeader(),
      '#rows' => $rows,
    );
105
106
107
    return $form;
  }

108
109
110
111
112
113
114
115
116
117
  /**
   * 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.'));
    }
  }

118
119
120
121
  /**
   * Overrides \Drupal\system\Plugin\PluginUIBase::formSubmit().
   */
  public function formSubmit($form, &$form_state) {
122
    $form_state['redirect'] = 'admin/structure/block/add/' . $form_state['values']['block'] . '/' . $form_state['values']['theme'];
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  }

  /**
   * 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();
147
    $row[] = check_plain($display_plugin_definition['admin_label']);
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
227
228
229
    $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';
  }

}