Commit b568a4d5 authored by alexpott's avatar alexpott
Browse files

Issue #2456707 by dawehner, larowlan: Block Content views field handlers need...

Issue #2456707 by dawehner, larowlan: Block Content views field handlers need to be replaced with entity-aware handlers
parent 0fafbc39
# Schema for the views plugins of the Block Content module.
# @todo Fix this when https://www.drupal.org/node/2322949 is fixed.
views.field.block_content:
type: views_field
label: 'Block Content'
mapping:
link_to_entity:
type: boolean
label: 'Link this field to the original piece of block content'
views.field.block_content_type:
type: views.field.block_content
label: 'Block content type'
mapping:
machine_name:
type: string
label: 'Output machine name'
......@@ -21,12 +21,12 @@ public function getViewsData() {
$data = parent::getViewsData();
$data['block_content_field_data']['id']['field']['id'] = 'block_content';
$data['block_content_field_data']['id']['field']['id'] = 'field';
$data['block_content_field_data']['info']['field']['id'] = 'block_content';
$data['block_content_field_data']['info']['field']['id'] = 'field';
$data['block_content_field_data']['info']['field']['link_to_entity default'] = TRUE;
$data['block_content_field_data']['type']['field']['id'] = 'block_content_type';
$data['block_content_field_data']['type']['field']['id'] = 'field';
// @todo Figure out the way to integrate this automatic in
// content_translation https://www.drupal.org/node/2410261.
......
<?php
/**
* @file
* Contains \Drupal\block_content\Plugin\views\field\BlockContent.
*/
namespace Drupal\block_content\Plugin\views\field;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Field handler to provide simple renderer that allows linking to a block_content.
* Definition terms:
* - link_to_entity default: Should this field have the checkbox
* "link to entity" enabled by default.
*
* @ingroup views_field_handlers
*
* @ViewsField("block_content")
*/
class BlockContent extends FieldPluginBase {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs a new BlockContent.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('language_manager'),
$container->get('entity.manager')
);
}
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
// Don't add the additional fields to groupby
if (!empty($this->options['link_to_entity'])) {
$this->additional_fields['id'] = array('table' => 'block_content', 'field' => 'id');
}
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_entity'] = array('default' => isset($this->definition['link_to_entity default']) ? $this->definition['link_to_entity default'] : FALSE);
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['link_to_entity'] = array(
'#title' => $this->t('Link this field to the original piece of block content'),
'#description' => $this->t("Enable to override this field's links."),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['link_to_entity']),
);
parent::buildOptionsForm($form, $form_state);
}
/**
* Prepares link to the block_content.
*
* @param string $data
* The XSS safe string for the link text.
* @param \Drupal\views\ResultRow $values
* The values retrieved from a single row of a view's query result.
*
* @return string
* Returns a string for the link text.
*/
protected function renderLink($data, ResultRow $values) {
if (!empty($this->options['link_to_entity']) && !empty($this->additional_fields['id'])) {
if ($data !== NULL && $data !== '') {
$id = $this->getValue($values, 'id');
$block_content = $this->entityManager->getStorage('block_content')->load($id);
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = $block_content->url();
if (isset($this->aliases['langcode'])) {
$languages = $this->languageManager->getLanguages();
$langcode = $this->getValue($values, 'langcode');
if (isset($languages[$langcode])) {
$this->options['alter']['language'] = $languages[$langcode];
}
else {
unset($this->options['alter']['language']);
}
}
}
else {
$this->options['alter']['make_link'] = FALSE;
}
}
return $data;
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
return $this->renderLink($this->sanitizeValue($value), $values);
}
}
<?php
/**
* @file
* Contains \Drupal\block_content\Plugin\views\field\Type.
*/
namespace Drupal\block_content\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
/**
* Field handler to translate a block content type into its readable form.
*
* @todo Remove this when https://www.drupal.org/node/2363811 is fixed.
*
* @ingroup views_field_handlers
*
* @ViewsField("block_content_type")
*/
class Type extends BlockContent {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['machine_name'] = array('default' => FALSE);
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['machine_name'] = array(
'#title' => $this->t('Output machine name'),
'#description' => $this->t('Display field as the block content type machine name.'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['machine_name']),
);
}
/**
* Renders block content type name.
*
* @param string $data
* The block content type machine_name.
* @param \Drupal\views\ResultRow $values
* The values retrieved from a single row of a view's query result.
*
* @return string
* The block content type as human readable name or machine_name.
*/
public function renderName($data, ResultRow $values) {
if ($this->options['machine_name'] != 1 && $data !== NULL && $data !== '') {
$type = $this->entityManager->getStorage('block_content_type')->load($data);
return $type ? $this->t($this->sanitizeValue($type->label())) : '';
}
return $this->sanitizeValue($data);
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
return $this->renderLink($this->renderName($value, $values), $values);
}
}
......@@ -27,11 +27,11 @@ public function testFieldType() {
$block_content = $this->createBlockContent();
$expected_result[] = array(
'id' => $block_content->id(),
'block_content_field_data_type' => $block_content->bundle(),
'type' => $block_content->bundle(),
);
$column_map = array(
'id' => 'id',
'block_content_field_data_type' => 'block_content_field_data_type',
'type:target_id' => 'type',
);
$view = Views::getView('test_field_type');
......
......@@ -41,7 +41,7 @@ display:
table: block_content
field: id
relationship: id
plugin_id: block_content
plugin_id: field
entity_type: block_content
entity_field: id
arguments:
......
......@@ -43,7 +43,7 @@ display:
table: block_content
field: id
relationship: revision_id
plugin_id: block_content
plugin_id: field
entity_type: block_content
entity_field: id
arguments:
......
......@@ -116,8 +116,7 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
link_to_entity: false
plugin_id: block_content
plugin_id: field
entity_type: block_content
entity_field: id
sorts:
......
......@@ -71,7 +71,6 @@ display:
html: false
hide_empty: false
empty_zero: false
link_to_entity: true
relationship: none
group_type: group
admin_label: ''
......@@ -87,8 +86,11 @@ display:
empty: ''
hide_alter_empty: true
entity_type: block_content
type: string
settings:
link_to_entity: true
entity_field: title
plugin_id: block_content
plugin_id: field
filters:
info:
id: info
......
......@@ -19,7 +19,7 @@ display:
field: type
id: type
table: block_content_field_data
plugin_id: block_content_type
plugin_id: field
entity_type: block_content
entity_field: type
display_plugin: default
......
......@@ -98,7 +98,11 @@ protected function assertIdenticalResultsetHelper($view, $expected_result, $colu
// For entity fields we don't have the raw value. Let's try to fetch it
// using the entity itself.
elseif (empty($value->$view_column) && isset($view->field[$expected_column]) && ($field = $view->field[$expected_column]) && $field instanceof Field) {
$row[$expected_column] = $field->getValue($value);
$column = NULL;
if (count(explode(':', $view_column)) == 2) {
$column = explode(':', $view_column)[1];
}
$row[$expected_column] = $field->getValue($value, $column);
}
}
$result[$key] = $row;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment