+++ /dev/null
-<?php
-
-namespace Drupal\Driver\Fields\Drupal8;
-
-/**
- * Entity Reference field handler for Drupal 8.
- */
-class EntityReferenceHandler extends AbstractHandler {
-
- /**
- * {@inheritdoc}
- */
- public function expand($values) {
- $return = array();
- $entity_type_id = $this->fieldInfo->getSetting('target_type');
- $entity_definition = \Drupal::entityManager()->getDefinition($entity_type_id);
- $label_key = $entity_definition->getKey('label');
-
- // Determine target bundle restrictions.
- $target_bundle_key = NULL;
- if (!$target_bundles = $this->getTargetBundles()) {
- $target_bundle_key = $entity_definition->getKey('bundle');
- }
-
- foreach ($values as $value) {
- $query = \Drupal::entityQuery($entity_type_id)->condition($label_key, $value);
- if ($target_bundles && $target_bundle_key) {
- $query->condition($target_bundle_key, $target_bundles, 'IN');
- }
- if ($entities = $query->execute()) {
- $return[] = array_shift($entities);
- }
- else {
- throw new \Exception(sprintf("No entity '%s' of type '%s' exists.", $value, $entity_type_id));
- }
- }
- return $return;
- }
-
- /**
- * Retrieves bundles for which the field is configured to reference.
- *
- * @return mixed
- * Array of bundle names, or NULL if not able to determine bundles.
- */
- protected function getTargetBundles() {
- $settings = $this->fieldConfig->getSettings();
- if (!empty($settings['handler_settings']['target_bundles'])) {
- return $settings['handler_settings']['target_bundles'];
- }
- }
-
-}