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

Render Refactoring #8

Open
wants to merge 2 commits into
base: 8.x-2.x
Choose a base branch
from
Open
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
53 changes: 0 additions & 53 deletions src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php

This file was deleted.

104 changes: 104 additions & 0 deletions src/Plugin/Field/FieldFormatter/ViewfieldFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* @file
* Contains \Drupal\viewfield\Plugin\Field\FieldFormatter\ViewfieldFormatter.
*/


namespace Drupal\viewfield\Plugin\Field\FieldFormatter;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\views\Views;

/**
* Plugin implementation of the 'viewfield_formatter' formatter.
*
* @FieldFormatter(
* id = "viewfield_formatter",
* label = @Translation("Viewfield formatter"),
* field_types = {
* "viewfield"
* }
* )
*/
class ViewfieldFormatter extends FormatterBase {

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();

foreach ($items as $delta => $item) {
/* @var $item \Drupal\Core\Field\FieldItemBase */
/* @var $entity \Drupal\Core\Entity\Entity */
$entity = $item->getEntity();
list($view_name, $view_display) = explode('|', $item->vname, 2);
$view = Views::getView($view_name);
$view_args = $this->getViewArgs($item->vargs, $entity);
// Build the view display's renderable array per item.
$elements[$delta] = $view->buildRenderable($view_display, $view_args);
$elements[$delta]['#access'] = $view && $view->access($view_display);
}
return $elements;
}

/**
* Parse argument string into an array and perform token replacements.
*
* @param string $view_args
* The argument string from the entity edit/add form.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to which the field is attached.
*
* @return array
* List of arguments ready to be passed to a rendering method for a view.
*/
protected function getViewArgs($view_args, EntityInterface $entity) {
$args = array();
$token_data = array($entity->getEntityTypeId() => $entity);

if (!empty($view_args)) {
$pos = 0;
while ($pos < strlen($view_args)) {
$found = FALSE;
// If string starts with a quote, start after quote and get everything
// before next quote.
if (strpos($view_args, '"', $pos) === $pos) {
if (($quote = strpos($view_args, '"', ++$pos)) !== FALSE) {
// Skip pairs of quotes.
while (!(($ql = strspn($view_args, '"', $quote)) & 1)) {
$quote = strpos($view_args, '"', $quote + $ql);
}
$args[] = str_replace('""', '"', substr($view_args, $pos, $quote + $ql - $pos - 1));
$pos = $quote + $ql + 1;
$found = TRUE;
}
}
elseif (($comma = strpos($view_args, ',', $pos)) !== FALSE) {
// Otherwise, get everything before next comma.
$args[] = substr($view_args, $pos, $comma - $pos);
// Skip to after comma and repeat
$pos = $comma + 1;
$found = TRUE;
}
if (!$found) {
$args[] = substr($view_args, $pos);
$pos = strlen($view_args);
}
}

$token = \Drupal::token();
foreach ($args as $key => $value) {
$args[$key] = $token->replace($value, $token_data);
}
}

return $args;
}

}
2 changes: 1 addition & 1 deletion src/Plugin/Field/FieldType/ViewfieldItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* label = @Translation("Viewfield"),
* description = @Translation("Viewfield field type. Stores view name and arguments."),
* default_widget = "viewfield_select",
* default_formatter = "viewfield_default"
* default_formatter = "viewfield_formatter"
* )
*/
class ViewfieldItem extends FieldItemBase {
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/ViewFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function testFieldCreation() {
->save();
entity_get_display('node', 'article', 'full')
->setComponent($field_name, array(
'type' => 'viewfield_default',
'type' => 'viewfield_formatter',
))
->save();

Expand Down
20 changes: 0 additions & 20 deletions templates/viewfield-formatter-default.html.twig

This file was deleted.

136 changes: 0 additions & 136 deletions viewfield.module

This file was deleted.