Example #1
0
 /**
  * {@inheritdoc}
  */
 protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode)
 {
     // Add the default image if needed.
     if ($items->isEmpty()) {
         $default_image = $this->getFieldSetting('default_image');
         // If we are dealing with a configurable field, look in both
         // instance-level and field-level settings.
         if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) {
             $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
         }
         if (!empty($default_image['uuid']) && ($file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']))) {
             // Clone the FieldItemList into a runtime-only object for the formatter,
             // so that the fallback image can be rendered without affecting the
             // field values in the entity being rendered.
             $items = clone $items;
             $items->setValue(array('target_id' => $file->id(), 'alt' => $default_image['alt'], 'title' => $default_image['title'], 'width' => $default_image['width'], 'height' => $default_image['height'], 'entity' => $file, '_loaded' => TRUE, '_is_default' => TRUE));
             $file->_referringItem = $items[0];
         }
     }
     return parent::getEntitiesToView($items, $langcode);
 }
 /**
  * Returns the referenced entities for display.
  *
  * The method takes care of:
  * - checking entity access,
  * - placing the entities in the language expected for display.
  * It is thus strongly recommended that formatters use it in their
  * implementation of viewElements($items) rather than dealing with $items
  * directly.
  *
  * For each entity, the EntityReferenceItem by which the entity is referenced
  * is available in $entity->_referringItem. This is useful for field types
  * that store additional values next to the reference itself.
  *
  * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
  *   The item list.
  *
  * @return \Drupal\Core\Entity\EntityInterface[]
  *   The array of referenced entities to display, keyed by delta.
  */
 protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items)
 {
     $entities = array();
     $parent_entity_langcode = $items->getEntity()->language()->getId();
     foreach ($items as $delta => $item) {
         // Ignore items where no entity could be loaded in prepareView().
         if (!empty($item->_loaded)) {
             $entity = $item->entity;
             // Set the entity in the correct language for display.
             if ($entity instanceof TranslatableInterface && $entity->hasTranslation($parent_entity_langcode)) {
                 $entity = $entity->getTranslation($parent_entity_langcode);
             }
             // Check entity access if needed.
             if (!$this->needsAccessCheck($item) || $entity->access('view')) {
                 // Add the referring item, in case the formatter needs it.
                 $entity->_referringItem = $items[$delta];
                 $entities[$delta] = $entity;
             }
         }
     }
     return $entities;
 }
 /**
  * Returns the referenced entities for display.
  *
  * The method takes care of:
  * - checking entity access,
  * - placing the entities in the language expected for display.
  * It is thus strongly recommended that formatters use it in their
  * implementation of viewElements($items) rather than dealing with $items
  * directly.
  *
  * For each entity, the EntityReferenceItem by which the entity is referenced
  * is available in $entity->_referringItem. This is useful for field types
  * that store additional values next to the reference itself.
  *
  * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
  *   The item list.
  *
  * @return \Drupal\Core\Entity\EntityInterface[]
  *   The array of referenced entities to display, keyed by delta.
  *
  * @see ::prepareView()
  */
 protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items)
 {
     $entities = array();
     $parent_entity_langcode = $items->getEntity()->language()->getId();
     foreach ($items as $delta => $item) {
         // Ignore items where no entity could be loaded in prepareView().
         if (!empty($item->_loaded)) {
             $entity = $item->entity;
             // Set the entity in the correct language for display.
             if ($entity instanceof TranslatableInterface && $entity->hasTranslation($parent_entity_langcode)) {
                 $entity = $entity->getTranslation($parent_entity_langcode);
             }
             $access = $this->checkAccess($entity);
             // Add the access result's cacheability, ::view() needs it.
             $item->_accessCacheability = CacheableMetadata::createFromObject($access);
             if ($access->isAllowed()) {
                 // Add the referring item, in case the formatter needs it.
                 $entity->_referringItem = $items[$delta];
                 $entities[$delta] = $entity;
             }
         }
     }
     return $entities;
 }
Example #4
0
 /**
  * Convert a multiple value entity reference field into a string of labels .
  *
  * @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $fieldItems
  *    The data from an entity reference field.
  */
 protected function makePlain(EntityReferenceFieldItemListInterface $fieldItems)
 {
     $referencedEntities = $fieldItems->referencedEntities();
     $plainEntities = '';
     foreach ($referencedEntities as $referencedEntity) {
         //if ($plainEntities !== '') {
         // $plainEntities .= '/';
         //}
         // It's important to add the slash even for single references,
         // otherwise an ER views exact match is triggered for typed in entries,
         // since a parentless entity's title field would be the same as its
         // children's ancestry_plain field.
         $plainEntities .= $referencedEntity->label() . '/';
     }
     return $plainEntities;
 }
Example #5
0
 /**
  * Convert a multiple value entity reference field into a string of labels .
  *
  * @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $fieldItems
  *    The data from an entity reference field.
  */
 protected function makePlain(EntityReferenceFieldItemListInterface $fieldItems)
 {
     $referencedEntities = $fieldItems->referencedEntities();
     $plainEntities = '';
     foreach ($referencedEntities as $referencedEntity) {
         if ($plainEntities !== '') {
             $plainEntities .= '/';
         }
         $plainEntities .= $referencedEntity->label();
     }
     return $plainEntities;
 }