本文整理汇总了PHP中Drupal\Core\Field\FieldItemListInterface::getEntity方法的典型用法代码示例。如果您正苦于以下问题:PHP FieldItemListInterface::getEntity方法的具体用法?PHP FieldItemListInterface::getEntity怎么用?PHP FieldItemListInterface::getEntity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Field\FieldItemListInterface
的用法示例。
在下文中一共展示了FieldItemListInterface::getEntity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: checkFieldAccess
/**
* {@inheritdoc}
*/
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL)
{
// Fields that are not implicitly allowed to administrative users.
$explicit_check_fields = array('pass');
// Administrative users are allowed to edit and view all fields.
if (!in_array($field_definition->getName(), $explicit_check_fields) && $account->hasPermission('administer users')) {
return AccessResult::allowed()->cachePerPermissions();
}
// Flag to indicate if this user entity is the own user account.
$is_own_account = $items ? $items->getEntity()->id() == $account->id() : FALSE;
switch ($field_definition->getName()) {
case 'name':
// Allow view access to anyone with access to the entity. Anonymous
// users should be able to access the username field during the
// registration process, otherwise the username and email constraints
// are not checked.
if ($operation == 'view' || $items && $account->isAnonymous() && $items->getEntity()->isAnonymous()) {
return AccessResult::allowed()->cachePerPermissions();
}
// Allow edit access for the own user name if the permission is
// satisfied.
if ($is_own_account && $account->hasPermission('change own username')) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser();
} else {
return AccessResult::forbidden();
}
case 'preferred_langcode':
case 'preferred_admin_langcode':
case 'timezone':
case 'mail':
// Allow view access to own mail address and other personalization
// settings.
if ($operation == 'view') {
return $is_own_account ? AccessResult::allowed()->cachePerUser() : AccessResult::forbidden();
}
// Anyone that can edit the user can also edit this field.
return AccessResult::allowed()->cachePerPermissions();
case 'pass':
// Allow editing the password, but not viewing it.
return $operation == 'edit' ? AccessResult::allowed() : AccessResult::forbidden();
case 'created':
// Allow viewing the created date, but not editing it.
return $operation == 'view' ? AccessResult::allowed() : AccessResult::forbidden();
case 'roles':
case 'status':
case 'access':
case 'login':
case 'init':
return AccessResult::forbidden();
}
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}
示例3: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$element = [];
// As the Field API only applies the "field default value" to newly created
// entities, we'll apply the default value for existing entities.
if ($items->count() == 0) {
$field_default_value = $items->getFieldDefinition()->getDefaultValue($items->getEntity());
$items->status = $field_default_value[0]['status'];
}
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;
}
示例4: 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;
}
示例5: 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;
}
示例6: 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;
}
示例7: 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;
}
示例8: 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;
}
示例9: 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;
}
示例10: 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;
}
示例11: 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;
}
示例12: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$elements = [];
if ($items->getEntity()->getEntityTypeId() == 'aggregator_feed') {
$url_string = $items->getEntity()->getUrl();
} else {
$url_string = $items->getEntity()->getLink();
}
foreach ($items as $delta => $item) {
if ($this->getSetting('display_as_link') && $url_string) {
$elements[$delta] = ['#type' => 'link', '#title' => $item->value, '#url' => Url::fromUri($url_string)];
} else {
$elements[$delta] = ['#markup' => $item->value];
}
}
return $elements;
}
示例13: 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;
}
示例14: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items)
{
$entity = $items->getEntity();
// Add the first file as an enclosure to the RSS item. RSS allows only one
// enclosure per item. See: http://en.wikipedia.org/wiki/RSS_enclosure
foreach ($this->getEntitiesToView($items) as $delta => $file) {
$entity->rss_elements[] = array('key' => 'enclosure', 'attributes' => array('url' => file_create_url($file->getFileUri()), 'length' => $file->getSize(), 'type' => $file->getMimeType()));
}
}
示例15: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$parent_entity = $items->getEntity();
$elements = array();
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
$parent_entity->rss_elements[] = array('key' => 'category', 'value' => $entity->label(), 'attributes' => array('domain' => $entity->id() ? \Drupal::url('entity.taxonomy_term.canonical', ['taxonomy_term' => $entity->id()], array('absolute' => TRUE)) : ''));
}
return $elements;
}