Newer
Older
<?php
/**
* @file
Tim Plunkett
committed
* Definition of Drupal\field\Plugin\views\field\Field.
Tim Plunkett
committed
namespace Drupal\field\Plugin\views\field;
use Drupal\Core\Entity\EntityInterface;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
catch
committed
use Drupal\Core\Entity\EntityStorageInterface;
catch
committed
use Drupal\field\Field as FieldHelper;
catch
committed
use Drupal\Core\Entity\ContentEntityDatabaseStorage;
use Drupal\Core\Field\FieldDefinitionInterface;
Alex Pott
committed
use Drupal\Core\Field\FormatterPluginManager;
Alex Pott
committed
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager;
Angie Byron
committed
use Drupal\Core\Session\AccountInterface;
Alex Pott
committed
use Drupal\views\Views;
Tim Plunkett
committed
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* A field that displays fieldapi fields.
*
* @ingroup views_field_handlers
* @ViewsField("field")
*/
class Field extends FieldPluginBase {
Alex Pott
committed
* An array to store field renderable arrays for use by renderItems().
* @var array
*/
public $items = array();
/**
* The field information as returned by field_info_field().
* @var \Drupal\field\FieldConfigInterface
public $field_info;
/**
* Does the field supports multiple field values.
* @var bool
*/
public $multiple;
/**
* Does the rendered fields get limited.
* @var bool
*/
public $limit_values;
/**
* A shortcut for $view->base_table.
* @var string
*/
public $base_table;
Alex Pott
committed
/**
* An array of formatter options.
*
* @var array
*/
protected $formatterOptions;
Alex Pott
committed
/**
* The entity manager.
*
Angie Byron
committed
* @var \Drupal\Core\Entity\EntityManagerInterface
Alex Pott
committed
*/
protected $entityManager;
/**
* The field formatter plugin manager.
*
Alex Pott
committed
* @var \Drupal\Core\Field\FormatterPluginManager
*/
protected $formatterPluginManager;
Alex Pott
committed
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* Constructs a \Drupal\field\Plugin\views\field\Field object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Alex Pott
committed
* The field formatter plugin manager.
Alex Pott
committed
* @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager
* The field formatter plugin manager.
Alex Pott
committed
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
Angie Byron
committed
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager, FormatterPluginManager $formatter_plugin_manager, LanguageManager $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
Alex Pott
committed
$this->entityManager = $entity_manager;
$this->formatterPluginManager = $formatter_plugin_manager;
Alex Pott
committed
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
Alex Pott
committed
$container->get('entity.manager'),
Alex Pott
committed
$container->get('plugin.manager.field.formatter'),
$container->get('language_manager')
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::init().
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
catch
committed
$this->field_info = FieldHelper::fieldInfo()->getField($this->definition['entity_type'], $this->definition['field_name']);
$this->multiple = FALSE;
$this->limit_values = FALSE;
Alex Pott
committed
$cardinality = $this->field_info->getCardinality();
if ($this->field_info->isMultiple()) {
$this->multiple = TRUE;
// If "Display all values in the same row" is FALSE, then we always limit
// in order to show a single unique value per row.
if (!$this->options['group_rows']) {
$this->limit_values = TRUE;
}
// If "First and last only" is chosen, limit the values
if (!empty($this->options['delta_first_last'])) {
$this->limit_values = TRUE;
}
// Otherwise, we only limit values if the user hasn't selected "all", 0, or
// the value matching field cardinality.
Alex Pott
committed
if ((intval($this->options['delta_limit']) && ($this->options['delta_limit'] != $cardinality)) || intval($this->options['delta_offset'])) {
$this->limit_values = TRUE;
}
}
}
/**
Angie Byron
committed
* {@inheritdoc}
Angie Byron
committed
public function access(AccountInterface $account) {
$base_table = $this->get_base_table();
Alex Pott
committed
$access_controller = $this->entityManager->getAccessController($this->definition['entity_tables'][$base_table]);
Angie Byron
committed
return $access_controller->fieldAccess('view', $this->field_info, $account);
}
/**
* Set the base_table and base_table_alias.
*
* @return string
* The base table which is used in the current view "context".
*/
function get_base_table() {
if (!isset($this->base_table)) {
// This base_table is coming from the entity not the field.
Angie Byron
committed
$this->base_table = $this->view->storage->get('base_table');
// If the current field is under a relationship you can't be sure that the
// base table of the view is the base table of the current field.
// For example a field from a node author on a node view does have users as base table.
if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
Daniel Wehner
committed
$relationships = $this->view->display_handler->getOption('relationships');
if (!empty($relationships[$this->options['relationship']])) {
$options = $relationships[$this->options['relationship']];
Dries Buytaert
committed
$data = Views::viewsData()->get($options['table']);
$this->base_table = $data[$options['field']]['relationship']['base'];
}
}
}
return $this->base_table;
}
/**
* Called to add the field to a query.
*
Bojan Živanović
committed
* By default, all needed data is taken from entities loaded by the query
* plugin. Columns are added only if they are used in groupings.
public function query($use_groupby = FALSE) {
$this->get_base_table();
$entity_type = $this->definition['entity_tables'][$this->base_table];
$fields = $this->additional_fields;
Bojan Živanović
committed
// No need to add the entity type.
$entity_type_key = array_search('entity_type', $fields);
if ($entity_type_key !== FALSE) {
unset($fields[$entity_type_key]);
}
if ($use_groupby) {
// Add the fields that we're actually grouping on.
$options = array();
if ($this->options['group_column'] != 'entity_id') {
$options = array($this->options['group_column'] => $this->options['group_column']);
}
$options += is_array($this->options['group_columns']) ? $this->options['group_columns'] : array();
$fields = array();
catch
committed
$rkey = $this->definition['is revision'] ? EntityStorageInterface::FIELD_LOAD_REVISION : EntityStorageInterface::FIELD_LOAD_CURRENT;
// Go through the list and determine the actual column name from field api.
foreach ($options as $column) {
$name = $column;
Alex Pott
committed
if (isset($this->field_info['storage_details']['sql'][$rkey][$this->table][$column])) {
$name = $this->field_info['storage_details']['sql'][$rkey][$this->table][$column];
}
$fields[$column] = $name;
}
$this->group_fields = $fields;
}
// Add additional fields (and the table join itself) if needed.
if ($this->add_field_table($use_groupby)) {
$this->ensureMyTable();
$this->addAdditionalFields($fields);
// Filter by langcode, if field translation is enabled.
$field = $this->field_info;
if ($field->isTranslatable() && !empty($this->view->display_handler->options['field_langcode_add_to_query'])) {
$column = $this->tableAlias . '.langcode';
// By the same reason as field_language the field might be Language::LANGCODE_NOT_SPECIFIED in reality so allow it as well.
// @see this::field_langcode()
$default_langcode = language_default()->id;
Alex Pott
committed
$langcode = str_replace(
array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
array($this->languageManager->getCurrentLanguage(Language::TYPE_CONTENT), $default_langcode),
Alex Pott
committed
$this->view->display_handler->options['field_langcode']
);
$placeholder = $this->placeholder();
$langcode_fallback_candidates = $this->languageManager->getFallbackCandidates($langcode, array('operation' => 'views_query', 'data' => $this));
Alex Pott
committed
$this->query->addWhereExpression(0, "$column IN($placeholder) OR $column IS NULL", array($placeholder => $langcode_fallback_candidates));
}
}
}
/**
* Determine if the field table should be added to the query.
*/
function add_field_table($use_groupby) {
Bojan Živanović
committed
// Grouping is enabled.
if ($use_groupby) {
return TRUE;
}
// This a multiple value field, but "group multiple values" is not checked.
if ($this->multiple && !$this->options['group_rows']) {
return TRUE;
}
return FALSE;
}
/**
* Determine if this field is click sortable.
*/
public function clickSortable() {
// Not click sortable in any case.
if (empty($this->definition['click sortable'])) {
return FALSE;
}
// A field is not click sortable if it's a multiple field with
// "group multiple values" checked, since a click sort in that case would
// add a join to the field table, which would produce unwanted duplicates.
if ($this->multiple && $this->options['group_rows']) {
return FALSE;
}
return TRUE;
}
/**
* Called to determine what to tell the clicksorter.
*/
public function clickSort($order) {
// No column selected, can't continue.
if (empty($this->options['click_sort_column'])) {
return;
}
$this->ensureMyTable();
$field = field_info_field($this->definition['entity_type'], $this->definition['field_name']);
catch
committed
$column = ContentEntityDatabaseStorage::_fieldColumnName($field, $this->options['click_sort_column']);
if (!isset($this->aliases[$column])) {
// Column is not in query; add a sort on it (without adding the column).
$this->aliases[$column] = $this->tableAlias . '.' . $column;
Alex Pott
committed
$this->query->addOrderBy(NULL, NULL, $order, $this->aliases[$column]);
protected function defineOptions() {
$options = parent::defineOptions();
// defineOptions runs before init/construct, so no $this->field_info
$field = field_info_field($this->definition['entity_type'], $this->definition['field_name']);
Alex Pott
committed
$field_type = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field->getType());
Alex Pott
committed
$column_names = array_keys($field->getColumns());
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
$default_column = '';
// Try to determine a sensible default.
if (count($column_names) == 1) {
$default_column = $column_names[0];
}
elseif (in_array('value', $column_names)) {
$default_column = 'value';
}
// If the field has a "value" column, we probably need that one.
$options['click_sort_column'] = array(
'default' => $default_column,
);
$options['type'] = array(
'default' => $field_type['default_formatter'],
);
$options['settings'] = array(
'default' => array(),
);
$options['group_column'] = array(
'default' => $default_column,
);
$options['group_columns'] = array(
'default' => array(),
);
// Options used for multiple value fields.
$options['group_rows'] = array(
'default' => TRUE,
'bool' => TRUE,
);
// If we know the exact number of allowed values, then that can be
// the default. Otherwise, default to 'all'.
$options['delta_limit'] = array(
Alex Pott
committed
'default' => ($field->getCardinality() > 1) ? $field->getCardinality() : 'all',
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
);
$options['delta_offset'] = array(
'default' => 0,
);
$options['delta_reversed'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['delta_first_last'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['multi_type'] = array(
'default' => 'separator'
);
$options['separator'] = array(
'default' => ', '
);
$options['field_api_classes'] = array(
'default' => FALSE,
'bool' => TRUE,
);
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, &$form_state) {
parent::buildOptionsForm($form, $form_state);
$field = $this->field_info;
Alex Pott
committed
$formatters = $this->formatterPluginManager->getOptions($field->getType());
Alex Pott
committed
$column_names = array_keys($field->getColumns());
// If this is a multiple value field, add its options.
if ($this->multiple) {
$this->multiple_options_form($form, $form_state);
}
// No need to ask the user anything if the field has only one column.
Alex Pott
committed
if (count($field->getColumns()) == 1) {
$form['click_sort_column'] = array(
'#type' => 'value',
'#value' => isset($column_names[0]) ? $column_names[0] : '',
);
}
else {
$form['click_sort_column'] = array(
'#type' => 'select',
'#title' => t('Column used for click sorting'),
catch
committed
'#options' => array_combine($column_names, $column_names),
'#default_value' => $this->options['click_sort_column'],
'#description' => t('Used by Style: Table to determine the actual column to click sort the field on. The default is usually fine.'),
);
}
$form['type'] = array(
'#type' => 'select',
'#title' => t('Formatter'),
'#options' => $formatters,
'#default_value' => $this->options['type'],
'#ajax' => array(
'path' => views_ui_build_form_path($form_state),
Angie Byron
committed
'#submit' => array(array($this, 'submitTemporaryForm')),
'#executes_submit_callback' => TRUE,
);
$form['field_api_classes'] = array(
'#title' => t('Use field template'),
'#type' => 'checkbox',
'#default_value' => $this->options['field_api_classes'],
'#description' => t('If checked, field api classes will be added by field templates. This is not recommended unless your CSS depends upon these classes. If not checked, template will not be used.'),
'#fieldset' => 'style_settings',
'#weight' => 20,
);
if ($this->multiple) {
$form['field_api_classes']['#description'] .= ' ' . t('Checking this option will cause the group Display Type and Separator values to be ignored.');
}
// Get the currently selected formatter.
$format = $this->options['type'];
Alex Pott
committed
$settings = $this->options['settings'] + $this->formatterPluginManager->getDefaultSettings($format);
Dries Buytaert
committed
$options = array(
'field_definition' => $field,
Dries Buytaert
committed
'configuration' => array(
'type' => $format,
'settings' => $settings,
'label' => '',
'weight' => 0,
),
'view_mode' => '_custom',
);
// Get the settings form.
$settings_form = array('#value' => array());
Alex Pott
committed
if ($formatter = $this->formatterPluginManager->getInstance($options)) {
Dries Buytaert
committed
$settings_form = $formatter->settingsForm($form, $form_state);
}
$form['settings'] = $settings_form;
}
/**
* Provide options for multiple value fields.
*/
function multiple_options_form(&$form, &$form_state) {
$field = $this->field_info;
$form['multiple_field_settings'] = array(
'#type' => 'details',
'#title' => t('Multiple field settings'),
'#weight' => 5,
);
$form['group_rows'] = array(
'#title' => t('Display all values in the same row'),
'#type' => 'checkbox',
'#default_value' => $this->options['group_rows'],
'#description' => t('If checked, multiple values for this field will be shown in the same row. If not checked, each value in this field will create a new row. If using group by, please make sure to group by "Entity ID" for this setting to have any effect.'),
'#fieldset' => 'multiple_field_settings',
);
// Make the string translatable by keeping it as a whole rather than
// translating prefix and suffix separately.
list($prefix, $suffix) = explode('@count', t('Display @count value(s)'));
Alex Pott
committed
if ($field->getCardinality() == FieldDefinitionInterface::CARDINALITY_UNLIMITED) {
$type = 'textfield';
$options = NULL;
$size = 5;
}
else {
$type = 'select';
catch
committed
$range = range(1, $field->getCardinality());
$options = array_combine($range, $range);
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
$size = 1;
}
$form['multi_type'] = array(
'#type' => 'radios',
'#title' => t('Display type'),
'#options' => array(
'ul' => t('Unordered list'),
'ol' => t('Ordered list'),
'separator' => t('Simple separator'),
),
'#states' => array(
'visible' => array(
':input[name="options[group_rows]"]' => array('checked' => TRUE),
),
),
'#default_value' => $this->options['multi_type'],
'#fieldset' => 'multiple_field_settings',
);
$form['separator'] = array(
'#type' => 'textfield',
'#title' => t('Separator'),
'#default_value' => $this->options['separator'],
'#states' => array(
'visible' => array(
':input[name="options[group_rows]"]' => array('checked' => TRUE),
':input[name="options[multi_type]"]' => array('value' => 'separator'),
),
),
'#fieldset' => 'multiple_field_settings',
);
$form['delta_limit'] = array(
'#type' => $type,
'#size' => $size,
'#field_prefix' => $prefix,
'#field_suffix' => $suffix,
'#options' => $options,
'#default_value' => $this->options['delta_limit'],
'#prefix' => '<div class="container-inline">',
'#states' => array(
'visible' => array(
':input[name="options[group_rows]"]' => array('checked' => TRUE),
),
),
'#fieldset' => 'multiple_field_settings',
);
list($prefix, $suffix) = explode('@count', t('starting from @count'));
$form['delta_offset'] = array(
'#type' => 'textfield',
'#size' => 5,
'#field_prefix' => $prefix,
'#field_suffix' => $suffix,
'#default_value' => $this->options['delta_offset'],
'#states' => array(
'visible' => array(
':input[name="options[group_rows]"]' => array('checked' => TRUE),
),
),
'#description' => t('(first item is 0)'),
'#fieldset' => 'multiple_field_settings',
);
$form['delta_reversed'] = array(
'#title' => t('Reversed'),
'#type' => 'checkbox',
'#default_value' => $this->options['delta_reversed'],
'#suffix' => $suffix,
'#states' => array(
'visible' => array(
':input[name="options[group_rows]"]' => array('checked' => TRUE),
),
),
'#description' => t('(start from last values)'),
'#fieldset' => 'multiple_field_settings',
);
$form['delta_first_last'] = array(
'#title' => t('First and last only'),
'#type' => 'checkbox',
'#default_value' => $this->options['delta_first_last'],
'#suffix' => '</div>',
'#states' => array(
'visible' => array(
':input[name="options[group_rows]"]' => array('checked' => TRUE),
),
),
'#fieldset' => 'multiple_field_settings',
);
}
/**
* Extend the groupby form with group columns.
*/
public function buildGroupByForm(&$form, &$form_state) {
parent::buildGroupByForm($form, $form_state);
// With "field API" fields, the column target of the grouping function
// and any additional grouping columns must be specified.
catch
committed
catch
committed
$field_columns = array_keys($this->field_info->getColumns());
$group_columns = array(
'entity_id' => t('Entity ID'),
catch
committed
) + array_map('ucfirst', array_combine($field_columns, $field_columns));
$form['group_column'] = array(
'#type' => 'select',
'#title' => t('Group column'),
'#default_value' => $this->options['group_column'],
'#description' => t('Select the column of this field to apply the grouping function selected above.'),
'#options' => $group_columns,
);
catch
committed
$options = array(
'bundle' => 'Bundle',
'language' => 'Language',
'entity_type' => 'Entity_type',
);
// Add on defined fields, noting that they're prefixed with the field name.
$form['group_columns'] = array(
'#type' => 'checkboxes',
'#title' => t('Group columns (additional)'),
'#default_value' => $this->options['group_columns'],
'#description' => t('Select any additional columns of this field to include in the query and to group on.'),
'#options' => $options + $group_columns,
);
}
public function submitGroupByForm(&$form, &$form_state) {
parent::submitGroupByForm($form, $form_state);
$item = &$form_state['handler']->options;
// Add settings for "field API" fields.
$item['group_column'] = $form_state['values']['options']['group_column'];
$item['group_columns'] = array_filter($form_state['values']['options']['group_columns']);
}
/**
* Render all items in this field together.
*
* When using advanced render, each possible item in the list is rendered
* individually. Then the items are all pasted together.
*/
Alex Pott
committed
protected function renderItems($items) {
if (!empty($items)) {
if (!$this->options['group_rows']) {
return implode('', $items);
}
if ($this->options['multi_type'] == 'separator') {
return implode(filter_xss_admin($this->options['separator']), $items);
}
else {
$item_list = array(
'#theme' => 'item_list',
'#items' => $items,
'#title' => NULL,
'#list_type' => $this->options['multi_type'],
);
return drupal_render($item_list);
}
}
}
Bojan Živanović
committed
/**
* Return an array of items for the field.
*/
Alex Pott
committed
public function getItems($values) {
Alex Pott
committed
$original_entity = $this->getEntity($values);
Bojan Živanović
committed
if (!$original_entity) {
return array();
}
$entity = $this->process_entity($original_entity);
if (!$entity) {
return array();
}
Bojan Živanović
committed
$display = array(
'type' => $this->options['type'],
'settings' => $this->options['settings'],
'label' => 'hidden',
// Pass the View object in the display so that fields can act on it.
'views_view' => $this->view,
'views_field' => $this,
'views_row_id' => $this->view->row_index,
);
$render_array = $entity->get($this->definition['field_name'])->view($display);
Bojan Živanović
committed
$items = array();
if ($this->options['field_api_classes']) {
return array(array('rendered' => drupal_render($render_array)));
}
foreach (element_children($render_array) as $count) {
$items[$count]['rendered'] = $render_array[$count];
// FieldItemListInterface::view() adds an #access property to the render
// array that determines whether or not the current user is allowed to
// view the field in the context of the current entity. We need to respect
// this parameter when we pull out the children of the field array for
Bojan Živanović
committed
// rendering.
if (isset($render_array['#access'])) {
$items[$count]['rendered']['#access'] = $render_array['#access'];
}
// Only add the raw field items (for use in tokens) if the current user
// has access to view the field content.
if ((!isset($items[$count]['rendered']['#access']) || $items[$count]['rendered']['#access']) && !empty($render_array['#items'][$count])) {
$items[$count]['raw'] = $render_array['#items'][$count];
}
}
return $items;
}
/**
* Process an entity before using it for rendering.
*
* Replaces values with aggregated values if aggregation is enabled.
* Takes delta settings into account (@todo remove in #1758616).
*
* @param \Drupal\Core\Entity\EntityInterface $entity
Bojan Živanović
committed
* The entity to be processed.
*
* @return
* TRUE if the processing completed successfully, otherwise FALSE.
*/
function process_entity(EntityInterface $entity) {
Bojan Živanović
committed
$processed_entity = clone $entity;
$langcode = $this->field_langcode($processed_entity);
$processed_entity = $processed_entity->getTranslation($langcode);
// If we are grouping, copy our group fields into the cloned entity.
// It's possible this will cause some weirdness, but there's only
// so much we can hope to do.
if (!empty($this->group_fields)) {
// first, test to see if we have a base value.
$base_value = array();
// Note: We would copy original values here, but it can cause problems.
// For example, text fields store cached filtered values as
// 'safe_value' which doesn't appear anywhere in the field definition
// so we can't affect it. Other side effects could happen similarly.
$data = FALSE;
foreach ($this->group_fields as $field_name => $column) {
if (property_exists($values, $this->aliases[$column])) {
$base_value[$field_name] = $values->{$this->aliases[$column]};
if (isset($base_value[$field_name])) {
$data = TRUE;
}
}
}
// If any of our aggregated fields have data, fake it:
if ($data) {
// Now, overwrite the original value with our aggregated value.
// This overwrites it so there is always just one entry.
$processed_entity->{$this->definition['field_name']} = array($base_value);
$processed_entity->{$this->definition['field_name']} = array();
}
}
// The field we are trying to display doesn't exist on this entity.
Bojan Živanović
committed
if (!isset($processed_entity->{$this->definition['field_name']})) {
return FALSE;
}
// We are supposed to show only certain deltas.
Bojan Živanović
committed
if ($this->limit_values && !empty($processed_entity->{$this->definition['field_name']})) {
$all_values = !empty($processed_entity->{$this->definition['field_name']}) ? $processed_entity->{$this->definition['field_name']}->getValue() : array();
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
if ($this->options['delta_reversed']) {
$all_values = array_reverse($all_values);
}
// Offset is calculated differently when row grouping for a field is
// not enabled. Since there are multiple rows, the delta needs to be
// taken into account, so that different values are shown per row.
if (!$this->options['group_rows'] && isset($this->aliases['delta']) && isset($values->{$this->aliases['delta']})) {
$delta_limit = 1;
$offset = $values->{$this->aliases['delta']};
}
// Single fields don't have a delta available so choose 0.
elseif (!$this->options['group_rows'] && !$this->multiple) {
$delta_limit = 1;
$offset = 0;
}
else {
$delta_limit = $this->options['delta_limit'];
$offset = intval($this->options['delta_offset']);
// We should only get here in this case if there's an offset, and
// in that case we're limiting to all values after the offset.
if ($delta_limit == 'all') {
$delta_limit = count($all_values) - $offset;
}
}
// Determine if only the first and last values should be shown
$delta_first_last = $this->options['delta_first_last'];
$new_values = array();
for ($i = 0; $i < $delta_limit; $i++) {
$new_delta = $offset + $i;
if (isset($all_values[$new_delta])) {
// If first-last option was selected, only use the first and last values
if (!$delta_first_last
// Use the first value.
|| $new_delta == $offset
// Use the last value.
|| $new_delta == ($delta_limit + $offset - 1)) {
$new_values[] = $all_values[$new_delta];
}
}
}
$processed_entity->{$this->definition['field_name']} = $new_values;
Bojan Živanović
committed
return $processed_entity;
}
function render_item($count, $item) {
return render($item['rendered']);
}
protected function documentSelfTokens(&$tokens) {
$field = $this->field_info;
Alex Pott
committed
foreach ($field->getColumns() as $id => $column) {
$tokens['[' . $this->options['id'] . '-' . $id . ']'] = t('Raw @column', array('@column' => $id));
}
}
Alex Pott
committed
protected function addSelfTokens(&$tokens, $item) {
$field = $this->field_info;
Alex Pott
committed
foreach ($field->getColumns() as $id => $column) {
// Use filter_xss_admin because it's user data and we can't be sure it is safe.
// We know nothing about the data, though, so we can't really do much else.
if (isset($item['raw'])) {
// If $item['raw'] is an array then we can use as is, if it's an object
// we cast it to an array, if it's neither, we can't use it.
$raw = is_array($item['raw']) ? $item['raw'] :
(is_object($item['raw']) ? (array)$item['raw'] : NULL);
}
if (isset($raw) && isset($raw[$id]) && is_scalar($raw[$id])) {
$tokens['[' . $this->options['id'] . '-' . $id . ']'] = filter_xss_admin($raw[$id]);
}
else {
Jennifer Hodgdon
committed
// Make sure that empty values are replaced as well.
$tokens['[' . $this->options['id'] . '-' . $id . ']'] = '';
}
}
}
/**
* Return the language code of the language the field should be displayed in,
* according to the settings.
*/
function field_langcode(EntityInterface $entity) {
if ($this->field_info->isTranslatable()) {
$default_langcode = language_default()->id;
Alex Pott
committed
$langcode = str_replace(
array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
array($this->languageManager->getCurrentLanguage(Language::TYPE_CONTENT), $default_langcode),
Alex Pott
committed
$this->view->display_handler->options['field_language']
);
// Give the Entity Field API a chance to fallback to a different language
// (or Language::LANGCODE_NOT_SPECIFIED), in case the field has no data
// for the selected language. FieldItemListInterface::view() does this as
// well, but since the returned language code is used before calling it,
// the fallback needs to happen explicitly.
$langcode = $this->entityManager->getTranslationFromContext($entity, $langcode)->language()->id;
return $langcode;
return Language::LANGCODE_NOT_SPECIFIED;