diff --git a/README.txt b/README.txt index 3eb14fb..ee31158 100644 --- a/README.txt +++ b/README.txt @@ -1,21 +1,28 @@ This module allows you to embed a View as a field in another View. -The View in the view-field can accept argument values from other fields of the parent View, using tokens. +The View in the view-field can accept argument values from other fields of the +parent View, using tokens. Here's how: -1. Before you can add a view-field to a "parent" view, you must create a "child" view. -2. Add arguments to that child view. The parent view will be passing argument values to - the child so that the child knows what to display. No other settings are necessary, - but validators and "argument not present" could be set. +1. Before you can add a view-field to a "parent" view, + you must create a "child" view. +2. Add arguments to that child view. The parent view will be passing argument + values to the child so that the child knows what to display. No other + settings are necessary, but validators and "argument not present" could + before set. 3. Create a "parent" view, if not already existing. -4. Add child view (Global:View field). The field must be toward the bottom of the field list, - or at least underneath the fields that are going to be used as arguments. - (E.g., "node id" might be used as an match between the parent and child views, - so put the "node id" field before your Global:View field in the list.) -5. Select which View and Display to use for the child data (will require doing this in 2 steps - - the field must be saved before the display selection is available). -6. Find which tokens are available by looking at the "Replacement patterns" list right below the Arguments setting. - Type the token replacements, in order, separated by a comma, as the arguments for that view field. - These values will be passed to the child. Make sure each field you are passing as an argument is completely clean, - as in: no label, no formatting, nothing that would pass into the argument other than the desired text or number. +4. Add child view (Global:View field). The field must be toward the bottom of + the field list, or at least underneath the fields that are going to be used + as arguments. (E.g., "node id" might be used as an match between the parent + and child views, so put the "node id" field before your + Global:View field in the list.) +5. Select which View and Display to use for the child data (will require doing + this in 2 steps - the field must be saved before the display selection is + available). +6. Find which tokens are available by looking at the "Replacement patterns" + list right below the Arguments setting. Type the token replacements, + in order, separated by a comma, as the arguments for that view field. These + values will be passed to the child. Make sure each field you are passing + as an argument is completely clean, as in: no label, no formatting, nothing + that would pass into the argument other than the desired text or number. diff --git a/css/views_field_view.css b/css/views_field_view.css index 87c2da7..2c8335c 100644 --- a/css/views_field_view.css +++ b/css/views_field_view.css @@ -1,6 +1,6 @@ #edit-options-view-edit { margin: 15px 0px; - background-color: #F3F4EE; + background-color: #f3f4ee; padding: 5px; } diff --git a/src/Plugin/views/field/View.php b/src/Plugin/views/field/View.php index 2115af0..193b32b 100644 --- a/src/Plugin/views/field/View.php +++ b/src/Plugin/views/field/View.php @@ -13,6 +13,8 @@ use Drupal\views\Views; use Symfony\Component\DependencyInjection\ContainerInterface; /** + * View object class. + * * @ViewsField("view") */ class View extends FieldPluginBase { @@ -63,7 +65,7 @@ class View extends FieldPluginBase { /** * {@inheritdoc} * - * Only allow the field to be there, no other aggregation make sense + * Only allow the field to be there, no other aggregation make sense. */ public function buildGroupByForm(&$form, FormStateInterface $form_state) { parent::buildGroupByForm($form, $form_state); @@ -151,8 +153,12 @@ class View extends FieldPluginBase { // Provide a way to directly access the views edit link of the child view. // Don't show this link if the current view is the selected child view. if (!empty($this->options['view']) && !empty($this->options['display']) && ($this->view->storage->id() != $this->options['view'])) { - // use t() here, and set HTML on #link options. - $link_text = $this->t('Edit "%view (@display)" view', ['%view' => $view_options[$this->options['view']], '@display' => $this->options['display']]); + // Use t() here, and set HTML on #link options. + $link_text = $this->t('Edit "%view (@display)" view', + [ + '%view' => $view_options[$this->options['view']], + '@display' => $this->options['display'], + ]); $form['view_edit'] = [ '#type' => 'container', '#fieldset' => 'views_field_view', @@ -177,7 +183,7 @@ class View extends FieldPluginBase { '#suffix' => ']', ]; $form['view_edit']['description'] = [ - '#markup' => $this->t('Use this link to open the current child view\'s edit page in a new window.'), + '#markup' => $this->t("Use this link to open the current child view's edit page in a new window."), '#prefix' => '
', '#suffix' => '
', ]; @@ -236,7 +242,7 @@ class View extends FieldPluginBase { $view = Views::getView($this->options['view']); // Only execute and render the view if the user has access. - if ($view->access($this->options['display'])) { + if (NULL !== $view && $view->access($this->options['display'])) { $view->setDisplay($this->options['display']); if ($view->display_handler->isPagerEnabled()) { @@ -290,15 +296,15 @@ class View extends FieldPluginBase { * Gets field values from tokens. * * @param string $token - * The token string. E.g. explode(',', $this->options['args']); + * The token string. E.g. explode(',', $this->options['args']);. * @param \Drupal\views\ResultRow $values - * The values retrieved from a single row of a view's query result. + * The values retrieved from a single row of a view's query result. * @param \Drupal\views\ViewExecutable $view - * The full view object to get token values from. + * The full view object to get token values from. * * @return array - * An array of raw argument values, returned in the same order as the token - * were passed in. + * An array of raw argument values, returned in the same order as the token + * were passed in. */ public function getTokenValue($token, ResultRow $values, ViewExecutable $view) { $token_info = $this->getTokenArgument($token); @@ -311,15 +317,19 @@ class View extends FieldPluginBase { case 'raw_fields': $value = $view->field[$id]->getValue($values); break; + case 'fields': $value = (string) $view->field[$id]->last_render; break; + case 'raw_arguments': $value = $view->args[array_flip(array_keys($view->argument))[$id]]; break; + case 'arguments': $value = $view->argument[$id]->getTitle(); break; + default: $value = Html::escape(trim($token, '\'"')); } @@ -329,13 +339,15 @@ class View extends FieldPluginBase { /** * Return the argument type and raw argument from a token. - * E.g. {{ raw_arguments.null }} will return "array('type' => 'raw_arguments', 'id' => null)". + * + * E.g. {{ raw_arguments.null }} will return "array('type' => 'raw_arguments', + * 'id' => null)". * * @param string $token - * A single token string. + * A single token string. * * @return array - * An array containing type and arg (As described above). + * An array containing type and arg (As described above). */ protected function getTokenArgument($token) { // Trim whitespace and remove the brackets around the token. @@ -349,6 +361,7 @@ class View extends FieldPluginBase { /** * Returns array of tokens/values to be used in child views. + * * String containing tokens is split on either "," or "/" characters. * * @param string $token_string @@ -381,9 +394,6 @@ class View extends FieldPluginBase { } } - // This lets us prepare the key as we want it printed. - $count = 0; - foreach ($this->view->display_handler->getHandlers('argument') as $id => $handler) { $options[(string) $this->t('Arguments')]["{{ arguments.$id }}"] = $this->t('@argument title', ['@argument' => $handler->adminLabel()]); $options[(string) $this->t('Arguments')]["{{ raw_arguments.$id }}"] = $this->t('@argument input', ['@argument' => $handler->adminLabel()]); @@ -413,7 +423,7 @@ class View extends FieldPluginBase { cause unexpected behaviour, as this will use the last output of the field. This could be re written output also. If no prefix is used in the token pattern, "raw_fields" / "raw_arguments" will be used as a default.') . - '

', + '

', ]; } else { diff --git a/tests/modules/views_field_view_test_config/views_field_view_test_config.info.yml b/tests/modules/views_field_view_test_config/views_field_view_test_config.info.yml index de15854..93aeed7 100644 --- a/tests/modules/views_field_view_test_config/views_field_view_test_config.info.yml +++ b/tests/modules/views_field_view_test_config/views_field_view_test_config.info.yml @@ -2,7 +2,6 @@ name: 'Views field view test Config' type: module description: 'Provides default views for tests.' package: Testing -version: VERSION core_version_requirement: ^9 || ^10 dependencies: - drupal:views