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..2b9c39e 100644 --- a/viewfield.module +++ b/viewfield.module @@ -24,113 +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'])); - $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; + if (!empty($element['#view_arguments'])) { + $variables['output'] = $element['#view']->buildRenderable($element['#view_display'], array($element['#view_arguments'])); } - // 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']); + $variables['output'] = $element['#view']->buildRenderable($element['#view_display']); } - 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; }