/** * Helper function to normalize field items. * * @param \Drupal\Core\Field\FieldItemListInterface $field * The field object. * @param string $format * The format. * @param array $context * The context array. * * @return array * The array of normalized field items. */ protected function normalizeFieldItems($field, $format, $context) { $normalized_field_items = array(); if (!$field->isEmpty()) { foreach ($field as $field_item) { $normalized_field_items[] = $this->serializer->normalize($field_item, $format, $context); } } return $normalized_field_items; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface $item */ $item = $items[$delta]; /** @var \Drupal\plugin\PluginType\PluginTypeInterface $plugin_type */ $plugin_type = $item->getPluginType(); $element = ['#delta' => $delta, '#field_definition' => $this->fieldDefinition, '#element_validate' => [[get_class(), 'validateFormElement']], '#plugin_type_id' => $plugin_type->getId(), '#plugin_selector_id' => $this->pluginDefinition['plugin_selector_id'], '#process' => [[get_class(), 'processFormElement']], '#selected_plugin' => $items->isEmpty() ? NULL : $items->get($delta)->getContainedPluginInstance()]; $element['plugin_selector'] = static::getPluginSelector($form_state, $element)->buildSelectorForm([], $form_state); return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $tags = array(); if (!$items->isEmpty()) { foreach ($items as $item) { $tags[] = isset($item->entity) ? $item->entity : entity_load('taxonomy_term', $item->target_id); } } $element += array('#type' => 'textfield', '#default_value' => taxonomy_implode_tags($tags), '#autocomplete_route_name' => $this->getSetting('autocomplete_route_name'), '#autocomplete_route_parameters' => array('entity_type' => $items->getEntity()->getEntityTypeId(), 'field_name' => $this->fieldDefinition->getName()), '#size' => $this->getSetting('size'), '#placeholder' => $this->getSetting('placeholder'), '#maxlength' => 1024, '#element_validate' => array('taxonomy_autocomplete_validate')); return $element; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { $elements = array(); if ($items->isEmpty()) { // For fields with no value, just add the configured "empty" value. $elements[0] = array('#markup' => $this->getSetting('test_empty_string')); } else { foreach ($items as $delta => $item) { // This formatter only needs to output raw for testing. $elements[$delta] = array('#markup' => $item->value); } } return $elements; }
/** * Gets the entity labels. */ protected function getLabels(FieldItemListInterface $items, $delta) { if ($items->isEmpty()) { return array(); } $entity_labels = array(); // Load those entities and loop through them to extract their labels. $entities = entity_load_multiple($this->getFieldSetting('target_type'), $this->getEntityIds($items, $delta)); foreach ($entities as $entity_id => $entity_item) { $label = $entity_item->label(); $key = "{$label} ({$entity_id})"; // Labels containing commas or quotes must be wrapped in quotes. $key = Tags::encode($key); $entity_labels[] = $key; } return $entity_labels; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { $elements = array(); if (!$items->isEmpty()) { $header = array(t('Attachment'), t('Size')); $rows = array(); foreach ($items as $delta => $item) { if ($item->isDisplayed() && $item->entity) { $rows[] = array(array('data' => array('#theme' => 'file_link', '#file' => $item->entity)), array('data' => format_size($item->entity->getSize()))); } } $elements[0] = array(); if (!empty($rows)) { $elements[0] = array('#theme' => 'table__file_formatter_table', '#header' => $header, '#rows' => $rows); } } return $elements; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $element = array(); // If there are no images, don't do anything else. if ($items->isEmpty()) { return array(); } $entity = $items->getEntity(); $field_instance = $items->getFieldDefinition(); $entity_type_id = $entity->getEntityTypeId(); $entity_id = $entity->id(); $field_name = $field_instance->getName(); $display_name = $this->viewMode; $add_js = TRUE; // Check for incompatible view modes - see issue #2217791 if ($display_name == 'search_result' || $display_name == 'search_index') { $add_js = FALSE; } // The gallery shown in preview view will only display field data from the // previously saved version (that is the only version the XML generation // methods will have access to). Display a warning because of this. if (!empty($entity->in_preview)) { drupal_set_message(t('Juicebox galleries may not display correctly in preview mode. Any edits made to gallery data will only be visible after all changes are saved.'), 'warning', FALSE); } // Generate xml details. $xml_route_info = array('route_name' => 'juicebox.xml_field', 'route_parameters' => array('entityType' => $entity_type_id, 'entityId' => $entity_id, 'fieldName' => $field_name, 'displayName' => $display_name), 'options' => array('query' => $this->request->query->all())); // Try building the gallery and its XML. try { // Initialize the gallery. $gallery = $this->juicebox->newGallery($xml_route_info['route_parameters']); // Build the gallery. $this->buildGallery($gallery, $items); // Build field-specific contextual links. $contextual = $this->buildContextualLinks($xml_route_info, $entity_type_id); // Create a render array with the gallery markup. $element[0] = $this->juicebox->buildEmbed($gallery, $this->getSettings(), $xml_route_info, $add_js, $this->isPseudoInstance(), $contextual); } catch (\Exception $e) { $message = 'Exception building Juicebox embed code for field: !message in %function (line %line of %file).'; watchdog_exception('juicebox', $e, $message); } return $element; }