-
Notifications
You must be signed in to change notification settings - Fork 19
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
Changes from all commits
e45a3a6
ebd4c44
ce911e4
525e75a
8e83e7c
0622d0a
c8a0f54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
use Drupal\Core\Render\Element; | ||
|
||
/** | ||
* Renders a view of the selected view. | ||
* | ||
* @RenderElement("viewfield") | ||
*/ | ||
class Viewfield extends RenderElement { | ||
|
||
public function getInfo() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs at least function documentation. I guess |
||
$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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.