/** * {@inheritdoc} */ protected function viewValue(FieldItemInterface $item) { $value = $item->value; if ($this->getSetting('filemime_image') && $value) { $file_icon = ['#theme' => 'image__file_icon', '#file' => $item->getEntity()]; return $file_icon; } return $value; }
/** * Helper function to get Edit and Delete links for an item. */ protected function getEditLinks(FieldItemInterface $item) { $links = ''; if ($item->getEntity()->access('update', \Drupal::currentUser())) { $links = '(' . \Drupal::l(t('Edit'), Url::FromRoute('entity.field_collection_item.edit_form', array('field_collection_item' => $item->value))); $links .= '|' . \Drupal::l(t('Delete'), Url::FromRoute('entity.field_collection_item.delete_form', array('field_collection_item' => $item->value))); $links .= ')'; } return $links; }
/** * Returns the array of options for the widget. * * @param \Drupal\Core\Field\FieldItemInterface $item * The field item. * * @return array * The array of options for the widget. */ protected function getOptions(FieldItemInterface $item) { if (!isset($this->options)) { // Limit the settable options for the current user account. $options = $item->getSettableOptions(\Drupal::currentUser()); // Add an empty option if the widget needs one. if ($empty_option = $this->getEmptyOption()) { switch ($this->getPluginId()) { case 'options_buttons': $label = t('N/A'); break; case 'options_select': $label = $empty_option == static::OPTIONS_EMPTY_NONE ? t('- None -') : t('- Select a value -'); break; } $options = array('_none' => $label) + $options; } $module_handler = \Drupal::moduleHandler(); $context = array('fieldDefinition' => $this->fieldDefinition, 'entity' => $item->getEntity()); $module_handler->alter('options_list', $options, $context); array_walk_recursive($options, array($this, 'sanitizeLabel')); // Options might be nested ("optgroups"). If the widget does not support // nested options, flatten the list. if (!$this->supportsGroups()) { $options = $this->flattenOptions($options); } $this->options = $options; } return $this->options; }
/** * Returns the key value collection that should be used for the given entity. * @return string */ protected function getCollection() { return 'pathauto_state.' . $this->parent->getEntity()->getEntityTypeId(); }
/** * Get a translated version of the field item instance. * * To indicate that a field item applies to one translation of an entity and * not another, the property path must originate with a translation of the * entity. This is the reason for using target_instances, from which the * property path can be traversed up to the root. * * @param \Drupal\Core\Field\FieldItemInterface $field_item * The untranslated field item instance. * @param $langcode * The langcode. * * @return \Drupal\Core\Field\FieldItemInterface * The translated field item instance. */ protected function createTranslatedInstance(FieldItemInterface $item, $langcode) { // Remove the untranslated item that was created for the default language // by FieldNormalizer::denormalize(). $items = $item->getParent(); $delta = $item->getName(); unset($items[$delta]); // Instead, create a new item for the entity in the requested language. $entity = $item->getEntity(); $entity_translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode); $field_name = $item->getFieldDefinition()->getName(); return $entity_translation->get($field_name)->appendItem(); }
/** * {@inheritdoc} */ public function viewFieldItem(FieldItemInterface $item, $display = array()) { $entity = $item->getEntity(); $field_name = $item->getFieldDefinition()->getName(); // Clone the entity since we are going to modify field values. $clone = clone $entity; // Push the item as the single value for the field, and defer to viewField() // to build the render array for the whole list. $clone->{$field_name}->setValue(array($item->getValue())); $elements = $this->viewField($clone->{$field_name}, $display); // Extract the part of the render array we need. $output = isset($elements[0]) ? $elements[0] : array(); if (isset($elements['#access'])) { $output['#access'] = $elements['#access']; } return $output; }
/** * Get a translated version of the field item instance. * * To indicate that a field item applies to one translation of an entity and * not another, the property path must originate with a translation of the * entity. This is the reason for using target_instances, from which the * property path can be traversed up to the root. * * @param \Drupal\Core\Field\FieldItemInterface $field_item * The untranslated field item instance. * @param $langcode * The langcode. * * @return \Drupal\Core\Field\FieldItemInterface * The translated field item instance. */ protected function createTranslatedInstance(FieldItemInterface $field_item, $langcode) { $field_items = $field_item->getParent(); // Remove the untranslated instance from the field's list of items. $field_items->offsetUnset($field_item->getName()); // Get the entity in the requested language and the field's item list from // that. $entity_translation = $field_item->getEntity()->getTranslation($langcode); $field_items_translation = $entity_translation->get($field_item->getFieldDefinition()->getName()); // Create a new instance and return it. $count = $field_items_translation->isEmpty() ? 0 : $field_items_translation->count(); return $field_items_translation->get($count); }
/** * Gets the relation URI of the field containing an item. * * The relation URI is used as a property key when building the HAL structure. * * @param FieldItemInterface $field_item * The field item that is being normalized. * * @return string * The relation URI of the field. */ protected function getFieldRelationUri(FieldItemInterface $field_item) { $field_name = $field_item->getParent()->getName(); $entity = $field_item->getEntity(); return $this->linkManager->getRelationUri($entity->getEntityTypeId(), $entity->bundle(), $field_name); }