本文整理汇总了PHP中Drupal\Core\Field\FieldItemListInterface类的典型用法代码示例。如果您正苦于以下问题:PHP FieldItemListInterface类的具体用法?PHP FieldItemListInterface怎么用?PHP FieldItemListInterface使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FieldItemListInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items)
{
$element = array();
$entity = $items->getEntity();
$settings = $this->getSettings();
foreach ($items as $delta => $item) {
// By default use the full URL as the link text.
$url = $this->buildUrl($item);
$link_title = $url->toString();
// If the link text field value is available, use it for the text.
if (empty($settings['url_only']) && !empty($item->title)) {
// Unsanitized token replacement here because $options['html'] is FALSE
// by default in l().
$link_title = \Drupal::token()->replace($item->title, array($entity->getEntityTypeId() => $entity), array('sanitize' => FALSE, 'clear' => TRUE));
}
// The link_separate formatter has two titles; the link text (as in the
// field values) and the URL itself. If there is no link text value,
// $link_title defaults to the URL, so it needs to be unset.
// The URL version may need to be trimmed as well.
if (empty($item->title)) {
$link_title = NULL;
}
$url_title = $url->toString();
if (!empty($settings['trim_length'])) {
$link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
$url_title = truncate_utf8($url_title, $settings['trim_length'], FALSE, TRUE);
}
$element[$delta] = array('#theme' => 'link_formatter_link_separate', '#title' => $link_title, '#url_title' => $url_title, '#url' => $url);
}
return $element;
}
示例2: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
$OriginalValue = '';
if (($node = \Drupal::routeMatch()->getParameter('node')) && $node instanceof \Drupal\node\NodeInterface) {
$FiledsView = $items->view();
$LangCode = $items->getLangcode();
$FieldName = $FiledsView['#field_name'];
$node = (array) $node;
$arrayValues = array_values($node);
if (isset($arrayValues[0]['langcode']['x-default']) && $arrayValues[0]['langcode']['x-default'] != $LangCode) {
if ($FieldName != 'title') {
if (isset($arrayValues[0][$FieldName]['x-default'][0]['value'])) {
$OriginalValue = $arrayValues[0][$FieldName]['x-default'][0]['value'];
}
} else {
if (isset($arrayValues[0][$FieldName]['x-default'])) {
$OriginalValue = $arrayValues[0][$FieldName]['x-default'];
}
}
$Title = $OriginalValue;
$OriginalValue = Unicode::truncate($OriginalValue, 200, TRUE);
$OriginalValue = '<div class="original_text" title="' . $Title . '"><span class="original">ORIGINAL: </span>' . $OriginalValue . '</div>';
}
}
$element['value'] = $element + array('#type' => 'textfield', '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, '#size' => $this->getSetting('size'), '#placeholder' => $this->getSetting('placeholder'), '#maxlength' => $this->getFieldSetting('max_length'), '#attributes' => array('class' => array('text-full')), '#suffix' => $OriginalValue);
return $element;
}
示例3: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
$field_settings = $this->getFieldSettings();
// The field settings include defaults for the field type. However, this
// widget is a base class for other widgets (e.g., ImageWidget) that may act
// on field types without these expected settings.
$field_settings += array('display_default' => NULL, 'display_field' => NULL, 'description_field' => NULL);
$cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
$defaults = array('fids' => array(), 'display' => (bool) $field_settings['display_default'], 'description' => '');
// Essentially we use the managed_file type, extended with some
// enhancements.
$element_info = $this->elementInfo->getInfo('embridge_asset');
$element += array('#type' => 'embridge_asset', '#upload_location' => $items[$delta]->getUploadLocation(), '#upload_validators' => $items[$delta]->getUploadValidators(), '#value_callback' => array(get_class($this), 'value'), '#process' => array_merge($element_info['#process'], array(array(get_class($this), 'process'))), '#progress_indicator' => $this->getSetting('progress_indicator'), '#extended' => TRUE, '#entity_type' => $items->getEntity()->getEntityTypeId(), '#field_name' => $this->fieldDefinition->getName(), '#field_config' => $this->fieldDefinition->id(), '#allow_search' => $field_settings['allow_search'], '#display_field' => (bool) $field_settings['display_field'], '#display_default' => $field_settings['display_default'], '#description_field' => $field_settings['description_field'], '#cardinality' => $cardinality, '#catalog_id' => $field_settings['catalog_id'], '#library_id' => $field_settings['library_id']);
$element['#weight'] = $delta;
// Field stores FID value in a single mode, so we need to transform it for
// form element to recognize it correctly.
if (!isset($items[$delta]->fids) && isset($items[$delta]->target_id)) {
$items[$delta]->fids = array($items[$delta]->target_id);
}
$element['#default_value'] = $items[$delta]->getValue() + $defaults;
$default_fids = $element['#extended'] ? $element['#default_value']['fids'] : $element['#default_value'];
if (empty($default_fids)) {
$file_upload_help = array('#theme' => 'file_upload_help', '#description' => $element['#description'], '#upload_validators' => $element['#upload_validators'], '#cardinality' => $cardinality);
$this->alterFileUploadHelpParameters($file_upload_help);
$element['#description'] = \Drupal::service('renderer')->renderPlain($file_upload_help);
$element['#multiple'] = $cardinality != 1 ? TRUE : FALSE;
if ($cardinality != 1 && $cardinality != -1) {
$element['#element_validate'] = array(array(get_class($this), 'validateMultipleCount'));
}
}
return $element;
}
示例4: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$element = array();
$settings = $this->getSettings();
$items_array = array();
foreach ($items as $item) {
$items_array[] = $item;
}
// Merge defaults from the formatters and ensure proper ordering.
$this->prepareFormatters($this->fieldDefinition->getType(), $settings['formatters']);
// Loop through each formatter in order.
foreach ($settings['formatters'] as $name => $options) {
// Run any unrendered items through the formatter.
$formatter_items = array_diff_key($items_array, $element);
$formatter_instance = $this->getFormatter($options);
$formatter_instance->prepareView(array($items->getEntity()->id() => $items));
if ($result = $formatter_instance->viewElements($items, $langcode)) {
// Only add visible content from the formatter's render array result
// that matches an unseen delta.
$visible_deltas = Element::getVisibleChildren($result);
$visible_deltas = array_intersect($visible_deltas, array_keys($formatter_items));
$element += array_intersect_key($result, array_flip($visible_deltas));
// If running this formatter completed the output for all items, then
// there is no need to loop through the rest of the formatters.
if (count($element) == count($items_array)) {
break;
}
}
}
// Ensure the resulting elements are ordered properly by delta.
ksort($element);
return $element;
}
示例5: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items)
{
$element = array();
$entity = $items->getEntity();
$settings = $this->getSettings();
foreach ($items as $delta => $item) {
// By default use the full URL as the link text.
$url = $this->buildUrl($item);
$link_title = $url->toString();
// If the title field value is available, use it for the link text.
if (empty($settings['url_only']) && !empty($item->title)) {
// Unsanitized token replacement here because $options['html'] is FALSE
// by default in l().
$link_title = \Drupal::token()->replace($item->title, array($entity->getEntityTypeId() => $entity), array('sanitize' => FALSE, 'clear' => TRUE));
}
// Trim the link text to the desired length.
if (!empty($settings['trim_length'])) {
$link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
}
if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
$element[$delta] = array('#markup' => String::checkPlain($link_title));
} else {
$element[$delta] = array('#type' => 'link', '#title' => $link_title, '#options' => $url->getOptions());
if ($url->isExternal()) {
$element[$delta]['#href'] = $url->getPath();
} else {
$element[$delta]['#route_name'] = $url->getRouteName();
$element[$delta]['#route_parameters'] = $url->getRouteParameters();
}
}
}
return $element;
}
示例6: viewElements
/**
* {@inheritdoc}
*
* TODO: Use $langcode.
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$element = array();
$settings = $this->getFieldSettings();
$count = 0;
// TODO: Is there a better way to get an accurate count of the
// items from the FieldItemList that doesn't count blank items?
// Possibly \Countable->count()?
$storage = \Drupal::entityTypeManager()->getStorage('field_collection_item');
foreach ($items as $delta => $item) {
if ($item->value !== NULL) {
$count++;
$field_collection_item = $storage->loadRevision($item->revision_id);
if ($field_collection_item->isDefaultRevision()) {
$links = \Drupal::l($this->fieldDefinition->getName() . ' ' . $delta, Url::FromRoute('entity.field_collection_item.canonical', array('field_collection_item' => $item->value)));
$links .= ' ' . $this->getEditLinks($item);
} else {
$links = \Drupal::l($this->fieldDefinition->getName() . ' ' . $delta, Url::FromRoute('field_collection_item.revision_show', ['field_collection_item' => $item->value, 'field_collection_item_revision' => $item->revision_id]));
}
$element[$delta] = array('#markup' => $links);
}
}
$cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
if ($cardinality == -1 || $count < $cardinality) {
$element['#suffix'] = '<ul class="action-links action-links-field-collection-add"><li>';
$element['#suffix'] .= $this->getAddLink($items->getEntity());
$element['#suffix'] .= '</li></ul>';
}
return $element;
}
示例7: getValueFromProperty
/**
* {@inheritdoc}
*/
public function getValueFromProperty(FieldItemListInterface $property, $delta = 0)
{
if ($property->getValue() && $property->getFieldDefinition()->getCardinality() == 1) {
return $property->referencedEntities()[0];
}
return $property->referencedEntities();
}
示例8: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode = NULL)
{
$elements = array();
// This field should be on a comment.
// Get the entity type of the entity this comment is attached to.
$entityType = $items->getEntity()->get('entity_type')->value;
foreach ($items as $delta => $item) {
if (!empty($item->right_rid)) {
$storage = \Drupal::entityManager()->getStorage($entityType);
$right_revision = $storage->loadRevision($item->right_rid);
$entity = $storage->load($right_revision->id());
if (!empty($item->left_rid)) {
// We have a pair of revisions
$left_revision = $storage->loadRevision($item->left_rid);
$plugin = $this->diffLayoutManager->createInstance('changes');
$elements[$delta] = $plugin->build($left_revision, $right_revision, $entity);
} else {
// We have just a single revision, the original of this entity.
// Only link to it if it is not the current revision.
if ($item->right_rid !== $entity->getRevisionId()) {
// Trigger exclusion of interactive items like on preview.
$right_revision->in_preview = TRUE;
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entityType);
$original = $view_builder->view($right_revision);
$elements[$delta] = ['#type' => 'details', '#title' => 'Original version'];
$elements[$delta]['original'] = $original;
}
}
}
}
return $elements;
}
示例9: extractFormValues
/**
* {@inheritdoc}
*/
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
{
if ($this->isDefaultValueWidget($form_state)) {
$items->filterEmptyItems();
return;
}
$field_name = $this->fieldDefinition->getName();
$path = array_merge($form['#parents'], array($field_name));
$submitted_values = $form_state->getValue($path);
$values = [];
foreach ($items as $delta => $value) {
$this->setIefId(sha1($items->getName() . '-ief-single-' . $delta));
/** @var \Drupal\Core\Entity\EntityInterface $entity */
if (!($entity = $form_state->get(['inline_entity_form', $this->getIefId(), 'entity']))) {
return;
}
$values[$submitted_values[$delta]['_weight']] = ['entity' => $entity];
}
// Sort items base on weights.
ksort($values);
$values = array_values($values);
// Let the widget massage the submitted values.
$values = $this->massageFormValues($values, $form, $form_state);
// Assign the values and remove the empty ones.
$items->setValue($values);
$items->filterEmptyItems();
// Put delta mapping in $form_state, so that flagErrors() can use it.
$field_name = $this->fieldDefinition->getName();
$field_state = WidgetBase::getWidgetState($form['#parents'], $field_name, $form_state);
foreach ($items as $delta => $item) {
$field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
unset($item->_original_delta, $item->_weight);
}
WidgetBase::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
}
示例10: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
$referenced_entities = $items->referencedEntities();
$element = parent::formElement($items, $delta, $element, $form, $form_state);
// If this is an existing (not new item).
if ($delta < count($referenced_entities)) {
// Mark element as being existing, not new item.
// Top level of the returned element must be called 'target_id',
// so we cannot create a container.
// Autocomplete element does some fancy processing to handle empty strings,
// so we must use an autocomplete element not a hidden or textfield element.
// But #states[#visible] does not seem to have an option to always hide.,
// and autocomplete elements don't seem to accept #attributes, so we must
// use #prefix and #suffix to add a class so that we can hide it.
$element['#prefix'] = '<div class="er-enhanced-existing">';
$element['#suffix'] = '</div>';
if ($this->getSetting('preview')) {
// Add preview.
$element['#prefix'] = '<div class="er-enhanced-existing er-enhanced-previewing">';
$element['#attached']['library'][] = 'ahs_er_enhanced/preview';
$entityTypeName = $referenced_entities[$delta]->getEntityType()->id();
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entityTypeName);
$preview = $view_builder->view($referenced_entities[$delta], $this->getSetting('preview_view_mode'));
$element['preview_container'] = ['#type' => 'container', '#attributes' => ['class' => ['er-enhanced-preview']], 'preview' => $preview];
// Add a remove link to the preview.
$element['remove'] = ['#markup' => '<a class="er-enhanced-remove" href="">' . t('Remove') . '</a>'];
$element['#attached']['library'][] = 'ahs_er_enhanced/remove';
}
} else {
$element['#prefix'] = '<div class="er-enhanced-new">';
$element['#suffix'] = '</div>';
}
return $element;
}
示例11: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
// Shows the "default fields" in the edit-type-field page, AND edit-fields on the article-edit-page
/** @var \Drupal\iframe\Plugin\Field\FieldType\IframeItem $item */
$item = $items[$delta];
$field_settings = $this->getFieldSettings();
$settings = $this->getSettings();
$settings += $field_settings;
$entity = $items->getEntity();
# pre fill with other attributes, (! last chance here !)
if (TRUE) {
# $entity->isNew() ?
foreach (self::defaultSettings() as $dkey => $dval) {
$ddval = isset($item->{$dkey}) ? $item->{$dkey} : (isset($settings[$dkey]) ? $settings[$dkey] : NULL);
$element[$dkey] = array('#type' => 'value', '#value' => is_null($ddval) ? NULL : (string) $ddval);
}
}
$title = isset($item->title) ? $item->title : (!empty($settings['title']) ? $settings['title'] : '');
$element['title'] = array('#type' => 'textfield', '#title' => t('IFrame Title'), '#placeholder' => '', '#default_value' => $title, '#size' => 80, '#maxlength' => 1024, '#weight' => 2);
$url = isset($item->url) && !empty($item->url) ? $item->url : (!empty($settings['url']) ? $settings['url'] : '');
$element['url'] = array('#type' => 'textfield', '#title' => t('IFrame URL'), '#placeholder' => 'http://', '#default_value' => $url, '#size' => 80, '#maxlength' => 1024, '#weight' => 1);
$width = isset($item->width) && !empty($item->width) ? $item->width : (isset($settings['width']) ? $settings['width'] : NULL);
$element['width'] = array('#title' => t('width of an iframe'), '#type' => 'textfield', '#default_value' => $width, '#description' => t('iframes need fix width and height, only numbers are allowed.'), '#maxlength' => 4, '#size' => 4, '#weight' => 3, '#required' => TRUE);
$height = isset($item->height) && !empty($item->height) ? $item->height : (isset($settings['height']) ? $settings['height'] : NULL);
$element['height'] = array('#type' => 'textfield', '#title' => t('height of an iframe'), '#default_value' => $height, '#description' => t('iframes need fix width and height, only numbers are allowed.'), '#maxlength' => 4, '#size' => 4, '#weight' => 4, '#required' => TRUE);
if ($settings['expose_class']) {
$element['class'] = array('#type' => 'textfield', '#title' => t('Additional CSS Class'), '#default_value' => isset($item->class) ? $item->class : NULL, '#description' => t('When output, this iframe will have this class attribute. Multiple classes should be separated by spaces.'), '#weight' => 5);
}
#$element['#title'] = 'IIfframe';
return $element;
}
示例12: generateFieldMetadata
/**
* {@inheritdoc}
*/
public function generateFieldMetadata(FieldItemListInterface $items, $view_mode)
{
$entity = $items->getEntity();
$field_name = $items->getFieldDefinition()->getName();
// Early-return if user does not have access.
$access = $this->accessChecker->accessEditEntityField($entity, $field_name);
if (!$access) {
return array('access' => FALSE);
}
// Early-return if no editor is available.
$formatter_id = EntityViewDisplay::collectRenderDisplay($entity, $view_mode)->getRenderer($field_name)->getPluginId();
$editor_id = $this->editorSelector->getEditor($formatter_id, $items);
if (!isset($editor_id)) {
return array('access' => FALSE);
}
// Gather metadata, allow the editor to add additional metadata of its own.
$label = $items->getFieldDefinition()->getLabel();
$editor = $this->editorManager->createInstance($editor_id);
$metadata = array('label' => String::checkPlain($label), 'access' => TRUE, 'editor' => $editor_id, 'aria' => t('Entity @type @id, field @field', array('@type' => $entity->getEntityTypeId(), '@id' => $entity->id(), '@field' => $label)));
$custom_metadata = $editor->getMetadata($items);
if (count($custom_metadata)) {
$metadata['custom'] = $custom_metadata;
}
return $metadata;
}
示例13: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$element = [];
if ($items->status == 1 && $this->currentUser->hasPermission('view disqus comments')) {
$element[] = ['#type' => 'disqus', '#url' => $items->getEntity()->toUrl('canonical', ['absolute' => TRUE])->toString(), '#title' => (string) $items->getEntity()->label(), '#identifier' => $items->identifier ?: "{$items->getEntity()->getEntityTypeId()}/{$items->getEntity()->id()}"];
}
return $element;
}
示例14: generateIds
/**
* Generates unique ids for the field items.
*
* @param \Drupal\Core\Field\FieldItemListInterface $items
* The field items.
* @return array
* Array of ids keyed by field item delta.
*/
protected function generateIds(FieldItemListInterface $items)
{
$entity = $items->getEntity();
$ids = array();
foreach ($items as $delta => $item) {
$ids[$delta] = implode('_', array($entity->getEntityTypeId(), $entity->bundle(), $entity->id(), $items->getFieldDefinition()->getName(), $delta));
}
return $ids;
}
示例15: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items)
{
$elements = array();
$entity = $items->getEntity();
foreach ($items as $item) {
$entity->rss_elements[] = array('key' => 'category', 'value' => $item->entity->label(), 'attributes' => array('domain' => $item->target_id ? url('taxonomy/term/' . $item->target_id, array('absolute' => TRUE)) : ''));
}
return $elements;
}