use Drupal\views\FieldAPIHandlerTrait;
use Drupal\views\Entity\Render\EntityFieldRenderer;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
+use Drupal\views\Plugin\DependentWithRemovalPluginInterface;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
*
* @ViewsField("field")
*/
-class EntityField extends FieldPluginBase implements CacheableDependencyInterface, MultiItemsFieldHandlerInterface {
+class EntityField extends FieldPluginBase implements CacheableDependencyInterface, MultiItemsFieldHandlerInterface, DependentWithRemovalPluginInterface {
use FieldAPIHandlerTrait;
use PluginDependencyTrait;
}
/**
- * Gets the field storage of the used field.
+ * Gets the field storage definition.
*
* @return \Drupal\Core\Field\FieldStorageDefinitionInterface
+ * The field storage definition used by this handler.
*/
protected function getFieldStorageDefinition() {
$entity_type_id = $this->definition['entity_type'];
$field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
- $field_storage = NULL;
// @todo Unify 'entity field'/'field_name' instead of converting back and
// forth. https://www.drupal.org/node/2410779
- if (isset($this->definition['field_name'])) {
- $field_storage = $field_storage_definitions[$this->definition['field_name']];
+ if (isset($this->definition['field_name']) && isset($field_storage_definitions[$this->definition['field_name']])) {
+ return $field_storage_definitions[$this->definition['field_name']];
}
- elseif (isset($this->definition['entity field'])) {
- $field_storage = $field_storage_definitions[$this->definition['entity field']];
+
+ if (isset($this->definition['entity field']) && isset($field_storage_definitions[$this->definition['entity field']])) {
+ return $field_storage_definitions[$this->definition['entity field']];
+ }
+
+ // The list of field storage definitions above does not include computed
+ // base fields, so we need to explicitly fetch a list of all base fields in
+ // order to support them.
+ // @see \Drupal\Core\Entity\EntityFieldManager::getFieldStorageDefinitions()
+ $base_fields = $this->entityManager->getBaseFieldDefinitions($entity_type_id);
+ if (isset($this->definition['field_name']) && isset($base_fields[$this->definition['field_name']])) {
+ return $base_fields[$this->definition['field_name']]->getFieldStorageDefinition();
}
- return $field_storage;
}
/**
*/
public function getValue(ResultRow $values, $field = NULL) {
$entity = $this->getEntity($values);
+ // Retrieve the translated object.
+ $translated_entity = $this->getEntityFieldRenderer()->getEntityTranslation($entity, $values);
+
// Some bundles might not have a specific field, in which case the entity
// (potentially a fake one) doesn't have it either.
/** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */
- $field_item_list = isset($entity->{$this->definition['field_name']}) ? $entity->{$this->definition['field_name']} : NULL;
+ $field_item_list = isset($translated_entity->{$this->definition['field_name']}) ? $translated_entity->{$this->definition['field_name']} : NULL;
if (!isset($field_item_list)) {
// There isn't anything we can do without a valid field.
}
}
+ /**
+ * {@inheritdoc}
+ */
+ public function onDependencyRemoval(array $dependencies) {
+ // See if this handler is responsible for any of the dependencies being
+ // removed. If this is the case, indicate that this handler needs to be
+ // removed from the View.
+ $remove = FALSE;
+ // Get all the current dependencies for this handler.
+ $current_dependencies = $this->calculateDependencies();
+ foreach ($current_dependencies as $group => $dependency_list) {
+ // Check if any of the handler dependencies match the dependencies being
+ // removed.
+ foreach ($dependency_list as $config_key) {
+ if (isset($dependencies[$group]) && array_key_exists($config_key, $dependencies[$group])) {
+ // This handlers dependency matches a dependency being removed,
+ // indicate that this handler needs to be removed.
+ $remove = TRUE;
+ break 2;
+ }
+ }
+ }
+ return $remove;
+ }
+
}