/**
  * {@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;
 }
 /**
  * {@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;
 }
 /**
  * {@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;
 }
Beispiel #4
0
 /**
  * {@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;
 }
Beispiel #5
0
 /**
  * {@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;
 }
 /**
  * {@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;
 }
 /**
  * {@inheritdoc}
  */
 public function getValueFromProperty(FieldItemListInterface $property, $delta = 0)
 {
     if ($property->getValue() && $property->getFieldDefinition()->getCardinality() == 1) {
         return $property->referencedEntities()[0];
     }
     return $property->referencedEntities();
 }
Beispiel #8
0
 /**
  * {@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;
 }
 /**
  * {@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);
 }
 /**
  * {@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;
 }
 /**
  * {@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;
 }
 /**
  * {@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;
 }
 /**
  * {@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;
 }
 /**
  * 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;
 }
 /**
  * {@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;
 }
 /**
  * {@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()));
     }
 }
 /**
  * {@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;
 }
 /**
  * {@inheritdoc}
  */
 protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL)
 {
     if ($operation == 'edit') {
         // Only users with the "administer comments" permission can edit
         // administrative fields.
         $administrative_fields = array('uid', 'status', 'created', 'date');
         if (in_array($field_definition->getName(), $administrative_fields, TRUE)) {
             return AccessResult::allowedIfHasPermission($account, 'administer comments');
         }
         // No user can change read-only fields.
         $read_only_fields = array('hostname', 'changed', 'cid', 'thread');
         // These fields can be edited during comment creation.
         $create_only_fields = ['comment_type', 'uuid', 'entity_id', 'entity_type', 'field_name', 'pid'];
         if ($items && ($entity = $items->getEntity()) && $entity->isNew() && in_array($field_definition->getName(), $create_only_fields, TRUE)) {
             // We are creating a new comment, user can edit create only fields.
             return AccessResult::allowedIfHasPermission($account, 'post comments')->addCacheableDependency($entity);
         }
         // We are editing an existing comment - create only fields are now read
         // only.
         $read_only_fields = array_merge($read_only_fields, $create_only_fields);
         if (in_array($field_definition->getName(), $read_only_fields, TRUE)) {
             return AccessResult::forbidden();
         }
         // If the field is configured to accept anonymous contact details - admins
         // can edit name, homepage and mail. Anonymous users can also fill in the
         // fields on comment creation.
         if (in_array($field_definition->getName(), ['name', 'mail', 'homepage'], TRUE)) {
             if (!$items) {
                 // We cannot make a decision about access to edit these fields if we
                 // don't have any items and therefore cannot determine the Comment
                 // entity. In this case we err on the side of caution and prevent edit
                 // access.
                 return AccessResult::forbidden();
             }
             /** @var \Drupal\comment\CommentInterface $entity */
             $entity = $items->getEntity();
             $commented_entity = $entity->getCommentedEntity();
             $anonymous_contact = $commented_entity->get($entity->getFieldName())->getFieldDefinition()->getSetting('anonymous');
             $admin_access = AccessResult::allowedIfHasPermission($account, 'administer comments');
             $anonymous_access = AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT && $account->hasPermission('post comments'))->cachePerPermissions()->cacheUntilEntityChanges($entity)->cacheUntilEntityChanges($field_definition->getConfig($commented_entity->bundle()))->cacheUntilEntityChanges($commented_entity);
             return $admin_access->orIf($anonymous_access);
         }
     }
     if ($operation == 'view') {
         $entity = $items ? $items->getEntity() : NULL;
         // Admins can view any fields except hostname, other users need both the
         // "access comments" permission and for the comment to be published. The
         // mail field is hidden from non-admins.
         $admin_access = AccessResult::allowedIf($account->hasPermission('administer comments') && $field_definition->getName() != 'hostname')->cachePerPermissions();
         $anonymous_access = AccessResult::allowedIf($account->hasPermission('access comments') && (!$entity || $entity->isPublished()) && !in_array($field_definition->getName(), array('mail', 'hostname'), TRUE))->cachePerPermissions();
         if ($entity) {
             $anonymous_access->cacheUntilEntityChanges($entity);
         }
         return $admin_access->orIf($anonymous_access);
     }
     return parent::checkFieldAccess($operation, $field_definition, $account, $items);
 }
Beispiel #19
0
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $element = array();
     $settings = $this->getSettings();
     foreach ($items as $delta => $item) {
         $element[$delta] = array('#theme' => 'youtube_video', '#video_id' => $item->video_id, '#entity_title' => $items->getEntity()->label(), '#settings' => $settings);
     }
     return $element;
 }
 /**
  * {@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)
 {
     $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);
 }
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $main_widget = $element + array('#type' => 'textfield', '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, '#placeholder' => $this->getSetting('placeholder'), '#attributes' => array('class' => array('geocoder-source')));
     $element['value'] = $main_widget;
     $source_field_id = 'edit-' . str_replace('_', '-', $items->getFieldDefinition()->getName()) . '-' . $delta . '-value';
     $destination_field_id = 'edit-' . str_replace('_', '-', $this->getSetting('destination_field')) . '-wrapper';
     $google_api_key = \Drupal::config('geocoder.google')->get('api_key');
     $element['#attached'] = array('library' => array(array('system', 'jquery.ui.autocomplete')), 'js' => array('sites/all/libraries/geocoder-js/dist/geocoder.js', drupal_get_path('module', 'geocoder') . '/js/geocoderWidget.js', array('data' => array('geocoder' => array('engine' => 'google', 'api_key' => $google_api_key, 'fields' => array(array('sourceField' => $source_field_id, 'destinationField' => $destination_field_id, 'sourceType' => 'geofield')))), 'type' => 'setting')));
     return $element;
 }
Beispiel #23
0
 /**
  * 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 viewElements(FieldItemListInterface $items, $langcode)
 {
     $elements = array();
     $link_setting = $this->getSetting('link_to');
     $entity = $items->getEntity();
     foreach ($this->getEntitiesToView($items, $langcode) as $delta => $asset) {
         $item = $asset->_referringItem;
         $elements[$delta] = array('#theme' => 'embridge_file_link', '#asset' => $asset, '#description' => $item->description, '#entity' => $entity, '#link_to' => $link_setting, '#cache' => array('tags' => $asset->getCacheTags()));
     }
     return $elements;
 }
 /**
  * {@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;
 }
Beispiel #26
0
 /**
  * {@inheritdoc}
  */
 public function isCompatible(FieldItemListInterface $items)
 {
     $field_definition = $items->getFieldDefinition();
     // This editor is incompatible with multivalued fields.
     if ($field_definition->getFieldStorageDefinition()->getCardinality() != 1) {
         return FALSE;
     }
     // This editor is compatible with formatted ("rich") text fields; but only
     // if there is a currently active text format and that text format is the
     // 'full_html' text format.
     return $items[0]->format === 'full_html';
 }
Beispiel #27
0
 /**
  * {@inheritdoc}
  */
 public function isCompatible(FieldItemListInterface $items)
 {
     $field_definition = $items->getFieldDefinition();
     // This editor is incompatible with multivalued fields.
     if ($field_definition->getFieldStorageDefinition()->getCardinality() != 1) {
         return FALSE;
     } elseif (in_array($field_definition->getType(), array('text', 'text_long', 'text_with_summary'), TRUE)) {
         return FALSE;
     } else {
         return TRUE;
     }
 }
 /**
  * {@inheritdoc}
  *
  * @todo The processed text logic is too coupled to text fields. Figure out
  *   how to generalize to other textual field types.
  */
 public function isCompatible(FieldItemListInterface $items)
 {
     $field_definition = $items->getFieldDefinition();
     // This editor is incompatible with multivalued fields.
     if ($field_definition->getFieldStorageDefinition()->getCardinality() != 1) {
         return FALSE;
     } elseif ($field_definition->getSetting('text_processing')) {
         return FALSE;
     } else {
         return TRUE;
     }
 }
Beispiel #29
0
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $element = array();
     $settings = $this->getSettings();
     foreach ($items as $delta => $item) {
         $element[$delta] = array('#theme' => 'youtube_video', '#video_id' => $item->video_id, '#entity_title' => $items->getEntity()->label(), '#settings' => $settings);
         if ($settings['youtube_size'] == 'responsive') {
             $element[$delta]['#attached']['library'][] = 'youtube/drupal.youtube.responsive';
         }
     }
     return $element;
 }
 /**
  * {@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);
 }