本文整理汇总了PHP中Drupal\Core\Field\FieldItemListInterface::referencedEntities方法的典型用法代码示例。如果您正苦于以下问题:PHP FieldItemListInterface::referencedEntities方法的具体用法?PHP FieldItemListInterface::referencedEntities怎么用?PHP FieldItemListInterface::referencedEntities使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Field\FieldItemListInterface
的用法示例。
在下文中一共展示了FieldItemListInterface::referencedEntities方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getValueFromProperty
/**
* {@inheritdoc}
*/
public function getValueFromProperty(FieldItemListInterface $property, $delta = 0)
{
if ($property->getValue() && $property->getFieldDefinition()->getCardinality() == 1) {
return $property->referencedEntities()[0];
}
return $property->referencedEntities();
}
示例2: 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;
}
示例3: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
$entity = $items->getEntity();
$referenced_entities = $items->referencedEntities();
$element += array('#type' => 'entity_autocomplete', '#target_type' => $this->getFieldSetting('target_type'), '#selection_handler' => $this->getFieldSetting('handler'), '#selection_settings' => $this->getFieldSetting('handler_settings'), '#validate_reference' => FALSE, '#maxlength' => 1024, '#default_value' => isset($referenced_entities[$delta]) ? $referenced_entities[$delta] : NULL, '#size' => $this->getSetting('size'), '#placeholder' => $this->getSetting('placeholder'));
if ($this->getSelectionHandlerSetting('auto_create')) {
$element['#autocreate'] = array('bundle' => $this->getAutocreateBundle(), 'uid' => $entity instanceof EntityOwnerInterface ? $entity->getOwnerId() : \Drupal::currentUser()->id());
}
return array('target_id' => $element);
}
示例4: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition->getName();
$parents = $form['#parents'];
$referenced_entities = $items->referencedEntities();
if (isset($referenced_entities[$delta])) {
$entity_label = EntityAutocomplete::getEntityLabels([$referenced_entities[$delta]]);
$id_prefix = implode('-', array_merge($parents, [$field_name, $delta]));
$element += [
'#type' => 'container',
'#attributes' => ['class' => ['form--inline']],
'target_id' => [
'#type' => 'item',
'#markup' => $entity_label,
'#default_value' => !$referenced_entities[$delta]->isNew() ? $referenced_entities[$delta]->id() : NULL,
],
'entity' => [
'#type' => 'value',
'#default_value' => $referenced_entities[$delta],
],
'remove' => [
'#type' => 'submit',
'#name' => strtr($id_prefix, '-', '_') . '_remove',
'#value' => t('Remove'),
'#attributes' => ['class' => ['remove-item-submit', 'align-right']],
'#submit' => [[get_class($this), 'removeSubmit']],
'#ajax' => [
'callback' => [get_class($this), 'getWidgetElementAjax'],
'wrapper' => $this->getWrapperId(),
'effect' => 'fade',
],
],
];
}
return $element;
}