Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added render class for Viewfield. #4

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/Render/Element/Viewfield.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* @file
* Contains \Drupal\Core\Render\Element\Viewfield.
*/

namespace Drupal\Core\Render\Element;
Copy link
Collaborator

@derhasi derhasi Sep 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not use core's namespace, but Drupal\viewfield\... instead.


use Drupal\Core\Render\Element;

/**
* Renders a view of the selected view.
*
* @RenderElement("viewfield")
*/
class Viewfield extends RenderElement {

public function getInfo() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs at least function documentation. I guess {@inheritdoc} should do its job here.

$class = get_class($this);
return array(
'#input' => TRUE,
'#pre_render' => array(
array($class, 'preRenderTextfield'),
),
'#theme' => 'viewfield_formatter_default',
'#post_render' => array(
array($class, 'postRenderViewfield'),
),
);
}

public static function preRenderViewfield($element) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs some proper function documentation and type declaration.

$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>');
$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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs some proper function documentation and type declaration too.

$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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There needs to be some parameter documentation.

$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;
}

}
109 changes: 3 additions & 106 deletions viewfield.module
Original file line number Diff line number Diff line change
Expand Up @@ -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>');
$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;
}