diff --git a/core/modules/views/src/Plugin/views/PluginBase.php b/core/modules/views/src/Plugin/views/PluginBase.php index 4204c61..c2d9f0f 100644 --- a/core/modules/views/src/Plugin/views/PluginBase.php +++ b/core/modules/views/src/Plugin/views/PluginBase.php @@ -15,7 +15,6 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase as ComponentPluginBase; use Drupal\Core\Render\Element; -use Drupal\Core\Render\RenderContext; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\ViewExecutable; use Symfony\Component\DependencyInjection\ContainerInterface; diff --git a/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php b/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php index 96c8aea..de1818d 100644 --- a/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php +++ b/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php @@ -250,7 +250,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#title_display' => 'invisible', '#default_value' => $this->options['title'], '#description' => $this->t('Override the view and other argument titles. You may use Twig syntax in this field.'), - // @todo: Need to show available tokens. '#states' => array( 'visible' => array( ':input[name="options[title_enable]"]' => array('checked' => TRUE), @@ -259,6 +258,23 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#fieldset' => 'argument_present', ); + $output = $this->getTokenHelp(); + $form['token_help'] = [ + '#type' => 'details', + '#title' => $this->t('Replacement patterns'), + '#value' => $output, + '#states' => [ + 'visible' => [ + [ + ':input[name="options[title_enable]"]' => ['checked' => TRUE], + ], + [ + ':input[name="options[exception][title_enable]"]' => ['checked' => TRUE], + ], + ], + ], + ]; + $form['specify_validation'] = array( '#type' => 'checkbox', '#title' => $this->t('Specify validation criteria'), @@ -349,6 +365,46 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ); } + /** + * Provide token help information for the argument. + * + * @return array + * A render array. + */ + protected function getTokenHelp() { + $output = []; + + foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) { + /** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $handler */ + $options[t('Arguments')]["{{ arguments.$arg }}"] = $this->t('@argument title', array('@argument' => $handler->adminLabel())); + $options[t('Arguments')]["{{ raw_arguments.$arg }}"] = $this->t('@argument input', array('@argument' => $handler->adminLabel())); + } + + // We have some options, so make a list. + if (!empty($options)) { + $output[] = [ + '#markup' => '

' . $this->t("The following replacement tokens are available for this argument.") . '

', + ]; + foreach (array_keys($options) as $type) { + if (!empty($options[$type])) { + $items = array(); + foreach ($options[$type] as $key => $value) { + $items[] = $key . ' == ' . $value; + } + $item_list = array( + '#theme' => 'item_list', + '#items' => $items, + '#list_type' => $type, + ); + $output[] = $item_list; + } + } + } + + return $output; + } + + public function validateOptionsForm(&$form, FormStateInterface $form_state) { $option_values = &$form_state->getValue('options'); if (empty($option_values)) { diff --git a/core/modules/views/src/Tests/Update/ArgumentPlaceholderUpdatePathTest.php b/core/modules/views/src/Tests/Update/ArgumentPlaceholderUpdatePathTest.php index 269844f..d123b5d 100644 --- a/core/modules/views/src/Tests/Update/ArgumentPlaceholderUpdatePathTest.php +++ b/core/modules/views/src/Tests/Update/ArgumentPlaceholderUpdatePathTest.php @@ -13,6 +13,8 @@ /** * Tests the argument placeholder update path. * + * @see views_update_8002() + * * @group views */ class ArgumentPlaceholderUpdatePathTest extends UpdatePathTestBase { @@ -27,6 +29,9 @@ protected function setDatabaseDumpFiles() { ]; } + /** + * Ensures that %1 and !1 are converted to twig tokens in existing views. + */ public function testArgumentPlaceholderUpdate() { $this->runUpdates(); $view = View::load('test_token_view'); diff --git a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php index 704e94c..65471b7 100644 --- a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php @@ -497,6 +497,67 @@ protected function setupTestField(array $options = []) { return $field; } + /** + * @covers ::getRenderTokens + */ + public function testGetRenderTokensWithoutFieldsAndArguments() { + $field = $this->setupTestField(); + + $this->display->expects($this->any()) + ->method('getHandlers') + ->willReturnMap([ + ['argument', []], + ['field', []], + ]); + + $this->assertEquals([], $field->getRenderTokens([])); + } + + /** + * @covers ::getRenderTokens + */ + public function testGetRenderTokensWithoutArguments() { + $field = $this->setupTestField(['id' => 'id']); + + $field->last_render = 'last rendered output'; + $this->display->expects($this->any()) + ->method('getHandlers') + ->willReturnMap([ + ['argument', []], + ['field', ['id' => $field]], + ]); + + $this->assertEquals(['{{ id }}' => 'last rendered output'], $field->getRenderTokens([])); + } + + /** + * @covers ::getRenderTokens + */ + public function testGetRenderTokensWithArguments() { + $field = $this->setupTestField(['id' => 'id']); + $field->view->args = ['argument value']; + $field->view->build_info['substitutions']['{{ arguments.name }}'] = 'argument value'; + + $argument = $this->getMockBuilder('\Drupal\views\Plugin\views\argument\ArgumentPluginBase') + ->disableOriginalConstructor() + ->getMock(); + + $field->last_render = 'last rendered output'; + $this->display->expects($this->any()) + ->method('getHandlers') + ->willReturnMap([ + ['argument', ['name' => $argument]], + ['field', ['id' => $field]], + ]); + + $expected = [ + '{{ id }}' => 'last rendered output', + '{{ arguments.name }}' => 'argument value', + '{{ raw_arguments.name }}' => 'argument value', + ]; + $this->assertEquals($expected, $field->getRenderTokens([])); + } + } class FieldPluginBaseTestField extends FieldPluginBase {