/** * Generate the output appropriate for one field item. * * @param \Drupal\Core\Field\FieldItemInterface $item * One field item. * * @return string * The textual output generated. */ protected function viewValue(FieldItemInterface $item) { $display_output = $item->executeDisplayCode(); if ($this->getSetting('sanitized')) { return nl2br(SafeMarkup::checkPlain($display_output)); } else { return nl2br($display_output); } }
/** * {@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; }
/** * Helper function to print the actual chart. * * @param $item \Drupal\Core\Field\FieldItemInterface * The field item * @param int $entity_id * The entity id * @param int $delta * The delta * * @return string $output * The field output. */ public function easychartPrintChart(FieldItemInterface $item, $entity_id, $delta) { $values = $item->getValue(); $output = []; // Verify csv being given. if (empty($values['csv'])) { return FALSE; } else { // Print a div for js to pick up & render chart. $output['markup'] = '<div class="easychart-embed--' . $entity_id . '-' . $delta . '"></div>'; // Add config to output. $output['config'] = $values['config']; // Add csv to output. $output['csv'] = !empty($values['csv']) ? $values['csv'] : ''; } return $output; }
/** * 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(); }
protected function getMainPropertyName(FieldItemInterface $data) { return $data->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName(); }
/** * 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); }
/** * {@inheritdoc} */ protected function viewValue(FieldItemInterface $item) { $settings = $this->getSettings(); $langcode = $item->value; // Do NOT use the languagemanager, since it only uses installed languages. // $language_manager = \Drupal::languageManager(); // $language = $language_manager->getLanguage($langcode); // Does not work for e.g. Danish (da). $language = new Language(array('id' => $langcode)); // Create the markup for this value. $markup = array(); /* if (!empty($settings['format']['icon']) && \Drupal::moduleHandler()->moduleExists('languageicons')) { // Add a language icon. We might better use languageicons_link_add(). // @TODO: doesn't work for the Widget, even though hook_options_list says the <img>-tab is allowed. $variables = array( 'language' => $language, // TODO: what happens if no icon for this language code. 'title' => $item->getName(), //['name'], ); $markup[] = theme_languageicons_icon($variables); } */ if (!empty($settings['format']['iso'])) { $markup[] = $langcode; } if (!empty($settings['format']['name'])) { $markup[] = t($language->getName()); } if (!empty($settings['format']['name_native'])) { // @todo: Create feature request to add function to D8 core. $markup[] = empty($settings['format']['name']) ? $item->getNativeName() : '(' . $item->getNativeName() . ')'; } if (empty($markup)) { $markup[] = t($language->getName()); } else { $markup = implode(' ', $markup); } // The text value has no text format assigned to it, so the user input // should equal the output, including newlines. return ['#context' => ['value' => $item->value], '#type' => 'processed_text', '#text' => $markup, '#format' => $item->format, '#langcode' => $langcode]; }
/** * 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); }