diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php index 2c36002..7fc2c86 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php @@ -318,7 +318,7 @@ public function globalTokenForm(&$form, &$form_state) { } $form['global_tokens'] = array( - '#type' => 'fieldset', + '#type' => 'details', '#title' => t('Available global token replacements'), '#collapsed' => TRUE, ); diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php index a8cfb8a..056aeef 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php @@ -82,60 +82,6 @@ public function buildOptionsForm(&$form, &$form_state) { } /** - * Form helper function to add tokenization form elements. - */ - public function tokenForm(&$form, &$form_state) { - $form['tokenize'] = array( - '#type' => 'checkbox', - '#title' => t('Use replacement tokens from the first row'), - '#default_value' => $this->options['tokenize'], - ); - - // Get a list of the available fields and arguments for token replacement. - $options = array(); - foreach ($this->view->display_handler->getHandlers('field') as $field => $handler) { - $options[t('Fields')]["[$field]"] = $handler->adminLabel(); - } - - $count = 0; // This lets us prepare the key as we want it printed. - foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) { - $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->adminLabel())); - $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->adminLabel())); - } - - if (!empty($options)) { - $form['tokens'] = array( - '#type' => 'details', - '#title' => t('Replacement patterns'), - '#collapsed' => TRUE, - '#id' => 'edit-options-token-help', - '#states' => array( - 'visible' => array( - ':input[name="options[tokenize]"]' => array('checked' => TRUE), - ), - ), - ); - $form['tokens']['help'] = array( - '#markup' => '

' . t('The following tokens are available. If you would like to have the characters \'[\' and \']\' use the html entity codes \'%5B\' or \'%5D\' or they will get replaced with empty space.') . '

', - ); - foreach (array_keys($options) as $type) { - if (!empty($options[$type])) { - $items = array(); - foreach ($options[$type] as $key => $value) { - $items[] = $key . ' == ' . $value; - } - $form['tokens']['tokens'] = array( - '#theme' => 'item_list', - '#items' => $items, - ); - } - } - } - - $this->globalTokenForm($form, $form_state); - } - - /** * Don't run a query */ public function query() { } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/Entity.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/Entity.php index d552932..c9065b1 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/Entity.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/Entity.php @@ -10,7 +10,6 @@ use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\ViewExecutable; use Drupal\Component\Annotation\PluginID; -use Drupal\views\Plugin\views\area\AreaPluginBase; /** * Provides an area handler which renders an entity in a certain view mode. @@ -19,7 +18,7 @@ * * @PluginID("entity") */ -class Entity extends AreaPluginBase { +class Entity extends TokenizeAreaPluginBase { /** * Stores the entity type of the result entities. @@ -37,20 +36,23 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o } /** - * Overrides \Drupal\views\Plugin\views\area\AreaPluginBase::defineOptions(). + * Overrides \Drupal\views\Plugin\views\area\TokenizeAreaPluginBase::defineOptions(). */ protected function defineOptions() { $options = parent::defineOptions(); + // Per default we enable tokenize, as this is the most common use case for + // this handler. + $options['tokenize']['default'] = TRUE; + $options['entity_id'] = array('default' => ''); $options['view_mode'] = array('default' => ''); - $options['tokenize'] = array('default' => TRUE, 'bool' => TRUE); return $options; } /** - * Overrides \Drupal\views\Plugin\views\area\AreaPluginBase::buildOptionsForm(). + * Overrides \Drupal\views\Plugin\views\area\TokenizeAreaPluginBase::buildOptionsForm(). */ public function buildOptionsForm(&$form, &$form_state) { parent::buildOptionsForm($form, $form_state); @@ -68,9 +70,6 @@ public function buildOptionsForm(&$form, &$form_state) { '#type' => 'textfield', '#default_value' => $this->options['entity_id'], ); - - // Add tokenization form elements. - $this->tokenForm($form, $form_state); } /** @@ -94,11 +93,7 @@ protected function buildViewModeOptions() { */ function render($empty = FALSE) { if (!$empty || !empty($this->options['empty'])) { - $entity_id = $this->options['entity_id']; - if ($this->options['tokenize']) { - $entity_id = $this->view->style_plugin->tokenizeValue($entity_id, 0); - } - $entity_id = $this->globalTokenReplace($entity_id); + $entity_id = $this->tokenizeValue($this->options['entity_id']); if ($entity = entity_load($this->entityType, $entity_id)) { return entity_view($entity, $this->options['view_mode']); } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php index dcc44b1..783f46a 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php @@ -16,16 +16,21 @@ * * @PluginID("text") */ -class Text extends AreaPluginBase { +class Text extends TokenizeAreaPluginBase { + /** + * Overrides \Drupal\views\Plugin\views\area\TokenizeAreaPluginBase::defineOptions(). + */ protected function defineOptions() { $options = parent::defineOptions(); $options['content'] = array('default' => '', 'translatable' => TRUE, 'format_key' => 'format'); $options['format'] = array('default' => NULL); - $options['tokenize'] = array('default' => FALSE, 'bool' => TRUE); return $options; } + /** + * Overrides \Drupal\views\Plugin\views\area\TokenizeAreaPluginBase::buildOptionsForm(). + */ public function buildOptionsForm(&$form, &$form_state) { parent::buildOptionsForm($form, $form_state); @@ -36,9 +41,6 @@ public function buildOptionsForm(&$form, &$form_state) { '#format' => isset($this->options['format']) ? $this->options['format'] : filter_default_format(), '#wysiwyg' => FALSE, ); - - // Add tokenization form elements. - $this->tokenForm($form, $form_state); } public function submitOptionsForm(&$form, &$form_state) { @@ -66,10 +68,7 @@ function render($empty = FALSE) { */ public function renderTextarea($value, $format) { if ($value) { - if ($this->options['tokenize']) { - $value = $this->view->style_plugin->tokenizeValue($value, 0); - } - return check_markup($this->globalTokenReplace($value), $format, '', FALSE); + return check_markup($this->tokenizeValue($value), $format, '', FALSE); } } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php index 86d8358..6e04eb2 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php @@ -16,15 +16,20 @@ * * @PluginID("text_custom") */ -class TextCustom extends AreaPluginBase { +class TextCustom extends TokenizeAreaPluginBase { + /** + * Overrides \Drupal\views\Plugin\views\area\TokenizeAreaPluginBase::defineOptions(). + */ protected function defineOptions() { $options = parent::defineOptions(); $options['content'] = array('default' => '', 'translatable' => TRUE); - $options['tokenize'] = array('default' => FALSE, 'bool' => TRUE); return $options; } + /** + * Overrides \Drupal\views\Plugin\views\area\TokenizeAreaPluginBase::buildOptionsForm(). + */ public function buildOptionsForm(&$form, &$form_state) { parent::buildOptionsForm($form, $form_state); @@ -33,13 +38,6 @@ public function buildOptionsForm(&$form, &$form_state) { '#default_value' => $this->options['content'], '#rows' => 6, ); - - // Add tokenization form elements. - $this->tokenForm($form, $form_state); - } - - // Empty, so we don't inherit submitOptionsForm from the parent. - public function submitOptionsForm(&$form, &$form_state) { } /** @@ -60,10 +58,7 @@ function render($empty = FALSE) { */ public function renderTextarea($value) { if ($value) { - if ($this->options['tokenize']) { - $value = $this->view->style_plugin->tokenizeValue($value, 0); - } - return $this->sanitizeValue($this->globalTokenReplace($value), 'xss_admin'); + return $this->sanitizeValue($this->tokenizeValue($value), 'xss_admin'); } } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/TokenizeAreaPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/TokenizeAreaPluginBase.php new file mode 100644 index 0000000..c17ccb5 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/TokenizeAreaPluginBase.php @@ -0,0 +1,114 @@ + FALSE, 'bool' => TRUE); + return $options; + } + + /** + * Overrides \Drupal\views\Plugin\views\area\AreaPluginBase::buildOptionsForm(). + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + + // Add tokenization form elements. + $this->tokenForm($form, $form_state); + } + + /** + * Form helper function to add tokenization form elements. + */ + public function tokenForm(&$form, &$form_state) { + $form['tokenize'] = array( + '#type' => 'checkbox', + '#title' => t('Use replacement tokens from the first row'), + '#default_value' => $this->options['tokenize'], + ); + + // Get a list of the available fields and arguments for token replacement. + $options = array(); + foreach ($this->view->display_handler->getHandlers('field') as $field => $handler) { + $options[t('Fields')]["[$field]"] = $handler->adminLabel(); + } + + $count = 0; // This lets us prepare the key as we want it printed. + foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) { + $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->adminLabel())); + $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->adminLabel())); + } + + if (!empty($options)) { + $form['tokens'] = array( + '#type' => 'details', + '#title' => t('Replacement patterns'), + '#open' => TRUE, + '#id' => 'edit-options-token-help', + '#states' => array( + 'visible' => array( + ':input[name="options[tokenize]"]' => array('checked' => TRUE), + ), + ), + ); + $form['tokens']['help'] = array( + '#markup' => '

' . t('The following tokens are available. If you would like to have the characters \'[\' and \']\' use the html entity codes \'%5B\' or \'%5D\' or they will get replaced with empty space.') . '

', + ); + foreach (array_keys($options) as $type) { + if (!empty($options[$type])) { + $items = array(); + foreach ($options[$type] as $key => $value) { + $items[] = $key . ' == ' . $value; + } + $form['tokens']['tokens'] = array( + '#theme' => 'item_list', + '#items' => $items, + ); + } + } + } + + $this->globalTokenForm($form, $form_state); + } + + /** + * Replaces value with special views tokens and global tokens. + * + * @param string $value + * The value to eventually tokenize. + * + * @return string + * Tokenized value if tokenize option is enabled. In any case global tokens + * will be replaced. + */ + public function tokenizeValue($value) { + if ($this->options['tokenize']) { + // @todo: add an option to select the row? + $value = $this->view->style_plugin->tokenize_value($value, 0); + } + // As we add the globalTokenForm() we also should replace the token here. + return $this->globalTokenReplace($value); + } + +} diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaEntityTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaEntityTest.php index 465ae17..b2669a1 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaEntityTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaEntityTest.php @@ -96,7 +96,7 @@ public function testEntityArea() { $this->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.'); $result = $this->xpath('//div[@class = "view-footer"]'); - $this->assertTrue(strpos(trim((string) $result[0]), $entities[1]->label()) !== FALSE, 'The rendered entity appears in the header of the view.'); + $this->assertTrue(strpos(trim((string) $result[0]), $entities[1]->label()) !== FALSE, 'The rendered entity appears in the footer of the view.'); $this->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.'); // Change the view mode of the area handler.