From e45a3a65f0a46eb11207bc188c65057cdee3f265 Mon Sep 17 00:00:00 2001 From: Lukas von Blarer Date: Thu, 9 Jun 2016 03:55:12 +0200 Subject: [PATCH 1/6] Adding cache tags --- src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php b/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php index c4b2b14..7374664 100644 --- a/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php @@ -46,7 +46,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { '#entity_id' => $entity->id(), '#entity' => $entity, '#theme' => 'viewfield_formatter_default', + '#cache' => [ + 'tags' => $view->getCacheTags() + ], ); + kint($elements); } return $elements; } From ebd4c4481c871e3d93450295ce7aa0e2dfc57313 Mon Sep 17 00:00:00 2001 From: Lukas von Blarer Date: Thu, 9 Jun 2016 12:16:06 +0200 Subject: [PATCH 2/6] Remove kint() --- src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php b/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php index 7374664..66397a7 100644 --- a/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php @@ -50,7 +50,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { 'tags' => $view->getCacheTags() ], ); - kint($elements); } return $elements; } From ce911e46b76201b040e0853fe9761f7462702365 Mon Sep 17 00:00:00 2001 From: Lukas von Blarer Date: Fri, 29 Jul 2016 14:08:54 +0200 Subject: [PATCH 3/6] Using buildRenderable() to add cache metadata --- .../Field/FieldFormatter/ViewfieldDefaultFormatter.php | 5 ++--- viewfield.module | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php b/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php index 66397a7..b0f1a13 100644 --- a/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php @@ -35,9 +35,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $entity = $item->getEntity(); list($view_name, $view_display) = explode('|', $item->vname, 2); $view = Views::getView($view_name); + $build = $view->buildRenderable($view_display, _viewfield_get_view_args($item->vargs, $entity->getEntityTypeId(), $entity)); $elements[$delta] = array( '#type' => 'viewfield', '#view' => $view, + '#build' => $build, '#access' => $view && $view->access($view_display), '#view_name' => $view_name, '#view_display' => $view_display, @@ -46,9 +48,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { '#entity_id' => $entity->id(), '#entity' => $entity, '#theme' => 'viewfield_formatter_default', - '#cache' => [ - 'tags' => $view->getCacheTags() - ], ); } return $elements; diff --git a/viewfield.module b/viewfield.module index 56d3c68..3a929ae 100644 --- a/viewfield.module +++ b/viewfield.module @@ -24,9 +24,7 @@ function viewfield_theme() { */ function viewfield_preprocess_viewfield_formatter_default(&$variables) { $element = $variables['element']; - $view_el = $element['#view']->preview($element['#view_display'], array($element['#view_arguments'])); - $output = render ($view_el); - $variables['output'] = $output; + $variables['output'] = $element['#build']; } /** From 525e75a127524903781604a988b9b3d4158b5187 Mon Sep 17 00:00:00 2001 From: Marcus Bernal Date: Thu, 4 Aug 2016 18:25:54 -0700 Subject: [PATCH 4/6] Added render class for Viewfield. Absorbed the following functions into the class: - viewfield_element_info() - viewfield_pre_render() - viewfield_post_render() - _viewfield_get_view_args() -- This was made getViewArgs() which is a protected method Left rendering for the theme. --- src/Render/Element/Viewfield.php | 116 +++++++++++++++++++++++++++++++ viewfield.module | 109 +---------------------------- 2 files changed, 117 insertions(+), 108 deletions(-) create mode 100644 src/Render/Element/Viewfield.php diff --git a/src/Render/Element/Viewfield.php b/src/Render/Element/Viewfield.php new file mode 100644 index 0000000..3d7b4c0 --- /dev/null +++ b/src/Render/Element/Viewfield.php @@ -0,0 +1,116 @@ + TRUE, + '#pre_render' => array( + array($class, 'preRenderTextfield'), + ), + '#theme' => 'viewfield_formatter_default', + '#post_render' => array( + array($class, 'postRenderViewfield'), + ), + ); + } + + public static function preRenderViewfield($element) { + $stack = &drupal_static('viewfield_stack', array()); + + // Abort rendering in case the view could not be loaded. + if (empty($element['#view'])) { + // @todo Output an error message? + $element['#printed'] = TRUE; + } + // Abort rendering in case of recursion. + elseif (isset($stack[$element['#entity_type']][$element['#entity_id']])) { + $element['#printed'] = TRUE; + } + // Otherwise, add the rendered entity to the stack to prevent recursion. + else { + $stack[$element['#entity_type']][$element['#entity_id']] = TRUE; + + // Override the view's path to the current path. Otherwise, exposed + // views filters would submit to the front page. + $url = Url::fromRoute(''); + $current_path = $url->toString(); + $element['#view']->override_path = $current_path; + + // @todo Store views arguments serialized. + $element['#view_arguments'] = Viewfield::getViewArgs($element['#view_arguments'], $element['#entity_type'], $element['#entity']); + } + return $element; + } + + public static function postRenderViewfield($content, $element) { + $stack = &drupal_static('viewfield_stack', array()); + unset($stack[$element['#entity_type']][$element['#entity_id']]); + return $content; + } + + /** + * Perform argument replacement + */ + protected function getViewArgs($vargs, $entity_type, $entity) { + $args = array(); + + if (!empty($vargs)) { + $pos = 0; + while ($pos < strlen($vargs)) { + $found = FALSE; + // If string starts with a quote, start after quote and get everything + // before next quote. + if (strpos($vargs, '"', $pos) === $pos) { + if (($quote = strpos($vargs, '"', ++$pos)) !== FALSE) { + // Skip pairs of quotes. + while (!(($ql = strspn($vargs, '"', $quote)) & 1)) { + $quote = strpos($vargs, '"', $quote + $ql); + } + $args[] = str_replace('""', '"', substr($vargs, $pos, $quote + $ql - $pos - 1)); + $pos = $quote + $ql + 1; + $found = TRUE; + } + } + elseif (($comma = strpos($vargs, ',', $pos)) !== FALSE) { + // Otherwise, get everything before next comma. + $args[] = substr($vargs, $pos, $comma - $pos); + // Skip to after comma and repeat + $pos = $comma + 1; + $found = TRUE; + } + if (!$found) { + $args[] = substr($vargs, $pos); + $pos = strlen($vargs); + } + } + + list($entity_id) = $entity->id(); + $entity = entity_load($entity_type, $entity_id); + $token_data = array($entity_type => $entity); + + $token = Drupal::token(); + foreach ($args as $key => $value) { + $args[$key] = $token->replace($value, $token_data); + } + } + + return $args; + } + +} diff --git a/viewfield.module b/viewfield.module index 56d3c68..5f3e20c 100644 --- a/viewfield.module +++ b/viewfield.module @@ -25,112 +25,5 @@ function viewfield_theme() { function viewfield_preprocess_viewfield_formatter_default(&$variables) { $element = $variables['element']; $view_el = $element['#view']->preview($element['#view_display'], array($element['#view_arguments'])); - $output = render ($view_el); - $variables['output'] = $output; -} - -/** - * Implements hook_element_info(). - */ -function viewfield_element_info() { - $types['viewfield'] = array( - '#pre_render' => array('viewfield_pre_render'), - '#theme' => 'viewfield_formatter_default', - '#post_render' => array('viewfield_post_render'), - ); - return $types; -} - -/** - * #pre_render callback for a viewfield field. - * - * @see viewfield_post_render() - */ -function viewfield_pre_render($element) { - $stack = &drupal_static('viewfield_stack', array()); - - // Abort rendering in case the view could not be loaded. - if (empty($element['#view'])) { - // @todo Output an error message? - $element['#printed'] = TRUE; - } - // Abort rendering in case of recursion. - elseif (isset($stack[$element['#entity_type']][$element['#entity_id']])) { - $element['#printed'] = TRUE; - } - // Otherwise, add the rendered entity to the stack to prevent recursion. - else { - $stack[$element['#entity_type']][$element['#entity_id']] = TRUE; - - // Override the view's path to the current path. Otherwise, exposed - // views filters would submit to the front page. - $url = Url::fromRoute(''); - $current_path = $url->toString(); - $element['#view']->override_path = $current_path; - - // @todo Store views arguments serialized. - $element['#view_arguments'] = _viewfield_get_view_args($element['#view_arguments'], $element['#entity_type'], $element['#entity']); - } - return $element; -} - -/** - * #post_render callback for a viewfield field. - * - * @see viewfield_pre_render() - */ -function viewfield_post_render($content, $element) { - $stack = &drupal_static('viewfield_stack', array()); - unset($stack[$element['#entity_type']][$element['#entity_id']]); - return $content; -} - - -/** - * Perform argument replacement - */ -function _viewfield_get_view_args($vargs, $entity_type, $entity) { - $args = array(); - - if (!empty($vargs)) { - $pos = 0; - while ($pos < strlen($vargs)) { - $found = FALSE; - // If string starts with a quote, start after quote and get everything - // before next quote. - if (strpos($vargs, '"', $pos) === $pos) { - if (($quote = strpos($vargs, '"', ++$pos)) !== FALSE) { - // Skip pairs of quotes. - while (!(($ql = strspn($vargs, '"', $quote)) & 1)) { - $quote = strpos($vargs, '"', $quote + $ql); - } - $args[] = str_replace('""', '"', substr($vargs, $pos, $quote + $ql - $pos - 1)); - $pos = $quote + $ql + 1; - $found = TRUE; - } - } - elseif (($comma = strpos($vargs, ',', $pos)) !== FALSE) { - // Otherwise, get everything before next comma. - $args[] = substr($vargs, $pos, $comma - $pos); - // Skip to after comma and repeat - $pos = $comma + 1; - $found = TRUE; - } - if (!$found) { - $args[] = substr($vargs, $pos); - $pos = strlen($vargs); - } - } - - list($entity_id) = $entity->id(); - $entity = entity_load($entity_type, $entity_id); - $token_data = array($entity_type => $entity); - - $token = Drupal::token(); - foreach ($args as $key => $value) { - $args[$key] = $token->replace($value, $token_data); - } - } - - return $args; + $variables['output'] = $view_el; } From 8e83e7cd086a65a1efa0014c5132dced4b86be9a Mon Sep 17 00:00:00 2001 From: seanbeaton Date: Fri, 16 Sep 2016 21:34:29 +0200 Subject: [PATCH 5/6] Allow views arguments to be unset. This is useful if you want to take the arguments from the path. --- viewfield.module | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/viewfield.module b/viewfield.module index 5f3e20c..eb6783c 100644 --- a/viewfield.module +++ b/viewfield.module @@ -24,6 +24,10 @@ function viewfield_theme() { */ function viewfield_preprocess_viewfield_formatter_default(&$variables) { $element = $variables['element']; - $view_el = $element['#view']->preview($element['#view_display'], array($element['#view_arguments'])); + if (isset($element['#view_arguments']) && $element['#view_arguments']) { + $view_el = $element['#view']->preview($element['#view_display'], array($element['#view_arguments'])); + } else { + $view_el = $element['#view']->preview($element['#view_display']); + } $variables['output'] = $view_el; } From c8a0f54426a2fce2d1ff7b3a8da5497b1f6a4ecc Mon Sep 17 00:00:00 2001 From: Marcus Bernal Date: Tue, 20 Sep 2016 10:30:45 -0700 Subject: [PATCH 6/6] Moved the building of a renderable array for a view. This is so that the arguments have had an opportunity to get aggregated by the Viewfield class. --- .../Field/FieldFormatter/ViewfieldDefaultFormatter.php | 2 -- viewfield.module | 7 ++++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php b/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php index b0f1a13..c4b2b14 100644 --- a/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php @@ -35,11 +35,9 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $entity = $item->getEntity(); list($view_name, $view_display) = explode('|', $item->vname, 2); $view = Views::getView($view_name); - $build = $view->buildRenderable($view_display, _viewfield_get_view_args($item->vargs, $entity->getEntityTypeId(), $entity)); $elements[$delta] = array( '#type' => 'viewfield', '#view' => $view, - '#build' => $build, '#access' => $view && $view->access($view_display), '#view_name' => $view_name, '#view_display' => $view_display, diff --git a/viewfield.module b/viewfield.module index 745ee99..2b9c39e 100644 --- a/viewfield.module +++ b/viewfield.module @@ -24,5 +24,10 @@ function viewfield_theme() { */ function viewfield_preprocess_viewfield_formatter_default(&$variables) { $element = $variables['element']; - $variables['output'] = $element['#build']; + if (!empty($element['#view_arguments'])) { + $variables['output'] = $element['#view']->buildRenderable($element['#view_display'], array($element['#view_arguments'])); + } + else { + $variables['output'] = $element['#view']->buildRenderable($element['#view_display']); + } }