/**
  * {@inheritdoc}
  */
 public function filterByFieldAccess(AccountInterface $account = NULL)
 {
     $filtered_fields = array();
     foreach ($this->getFieldNames() as $field_name) {
         if (!$this->entity->get($field_name)->access('edit', $account)) {
             $filtered_fields[] = $field_name;
         }
     }
     return $this->filterByFields($filtered_fields);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function buildCommentedEntityLinks(FieldableEntityInterface $entity, array &$context)
 {
     $entity_links = array();
     $view_mode = $context['view_mode'];
     if ($view_mode == 'search_index' || $view_mode == 'search_result' || $view_mode == 'print' || $view_mode == 'rss') {
         // Do not add any links if the entity is displayed for:
         // - search indexing.
         // - constructing a search result excerpt.
         // - print.
         // - rss.
         return array();
     }
     $fields = $this->commentManager->getFields($entity->getEntityTypeId());
     foreach ($fields as $field_name => $detail) {
         // Skip fields that the entity does not have.
         if (!$entity->hasField($field_name)) {
             continue;
         }
         $links = array();
         $commenting_status = $entity->get($field_name)->status;
         if ($commenting_status != CommentItemInterface::HIDDEN) {
             // Entity has commenting status open or closed.
             $field_definition = $entity->getFieldDefinition($field_name);
             if ($view_mode == 'teaser') {
                 // Teaser view: display the number of comments that have been posted,
                 // or a link to add new comments if the user has permission, the
                 // entity is open to new comments, and there currently are none.
                 if ($this->currentUser->hasPermission('access comments')) {
                     if (!empty($entity->get($field_name)->comment_count)) {
                         $links['comment-comments'] = array('title' => $this->formatPlural($entity->get($field_name)->comment_count, '1 comment', '@count comments'), 'attributes' => array('title' => $this->t('Jump to the first comment.')), 'fragment' => 'comments', 'url' => $entity->urlInfo());
                         if ($this->moduleHandler->moduleExists('history')) {
                             $links['comment-new-comments'] = array('title' => '', 'url' => Url::fromRoute('<current>'), 'attributes' => array('class' => 'hidden', 'title' => $this->t('Jump to the first new comment.'), 'data-history-node-last-comment-timestamp' => $entity->get($field_name)->last_comment_timestamp, 'data-history-node-field-name' => $field_name));
                         }
                     }
                 }
                 // Provide a link to new comment form.
                 if ($commenting_status == CommentItemInterface::OPEN) {
                     $comment_form_location = $field_definition->getSetting('form_location');
                     if ($this->currentUser->hasPermission('post comments')) {
                         $links['comment-add'] = array('title' => $this->t('Add new comments'), 'language' => $entity->language(), 'attributes' => array('title' => $this->t('Share your thoughts and opinions.')), 'fragment' => 'comment-form');
                         if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) {
                             $links['comment-add']['url'] = Url::fromRoute('comment.reply', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name]);
                         } else {
                             $links['comment-add'] += ['url' => $entity->urlInfo()];
                         }
                     } elseif ($this->currentUser->isAnonymous()) {
                         $links['comment-forbidden'] = array('title' => $this->commentManager->forbiddenMessage($entity, $field_name));
                     }
                 }
             } else {
                 // Entity in other view modes: add a "post comment" link if the user
                 // is allowed to post comments and if this entity is allowing new
                 // comments.
                 if ($commenting_status == CommentItemInterface::OPEN) {
                     $comment_form_location = $field_definition->getSetting('form_location');
                     if ($this->currentUser->hasPermission('post comments')) {
                         // Show the "post comment" link if the form is on another page, or
                         // if there are existing comments that the link will skip past.
                         if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE || !empty($entity->get($field_name)->comment_count) && $this->currentUser->hasPermission('access comments')) {
                             $links['comment-add'] = array('title' => $this->t('Add new comment'), 'attributes' => array('title' => $this->t('Share your thoughts and opinions.')), 'fragment' => 'comment-form');
                             if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) {
                                 $links['comment-add']['url'] = Url::fromRoute('comment.reply', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name]);
                             } else {
                                 $links['comment-add']['url'] = $entity->urlInfo();
                             }
                         }
                     } elseif ($this->currentUser->isAnonymous()) {
                         $links['comment-forbidden'] = array('title' => $this->commentManager->forbiddenMessage($entity, $field_name));
                     }
                 }
             }
         }
         if (!empty($links)) {
             $entity_links['comment__' . $field_name] = array('#theme' => 'links__entity__comment__' . $field_name, '#links' => $links, '#attributes' => array('class' => array('links', 'inline')));
             if ($view_mode == 'teaser' && $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated()) {
                 $entity_links['comment__' . $field_name]['#cache']['contexts'][] = 'user';
                 $entity_links['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link';
                 // Embed the metadata for the "X new comments" link (if any) on this
                 // entity.
                 $entity_links['comment__' . $field_name]['#attached']['drupalSettings']['history']['lastReadTimestamps'][$entity->id()] = (int) history_read($entity->id());
                 $new_comments = $this->commentManager->getCountNewComments($entity);
                 if ($new_comments > 0) {
                     $page_number = $this->entityManager->getStorage('comment')->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new_comments, $entity, $field_name);
                     $query = $page_number ? ['page' => $page_number] : NULL;
                     $value = ['new_comment_count' => (int) $new_comments, 'first_new_comment_link' => $entity->url('canonical', ['query' => $query, 'fragment' => 'new'])];
                     $parents = ['comment', 'newCommentsLinks', $entity->getEntityTypeId(), $field_name, $entity->id()];
                     NestedArray::setValue($entity_links['comment__' . $field_name]['#attached']['drupalSettings'], $parents, $value);
                 }
             }
         }
     }
     return $entity_links;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state)
 {
     // Set #parents to 'top-level' by default.
     $form += array('#parents' => array());
     // Let each widget generate the form elements.
     foreach ($this->getComponents() as $name => $options) {
         if ($widget = $this->getRenderer($name)) {
             $items = $entity->get($name);
             $items->filterEmptyItems();
             $form[$name] = $widget->form($items, $form, $form_state);
             $form[$name]['#access'] = $items->access('edit');
             // Assign the correct weight. This duplicates the reordering done in
             // processForm(), but is needed for other forms calling this method
             // directly.
             $form[$name]['#weight'] = $options['weight'];
             // Associate the cache tags for the field definition & field storage
             // definition.
             $field_definition = $this->getFieldDefinition($name);
             $this->renderer->addCacheableDependency($form[$name], $field_definition);
             $this->renderer->addCacheableDependency($form[$name], $field_definition->getFieldStorageDefinition());
         }
     }
     // Associate the cache tags for the form display.
     $this->renderer->addCacheableDependency($form, $this);
     // Add a process callback so we can assign weights and hide extra fields.
     $form['#process'][] = array($this, 'processForm');
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function getOptionsProvider($property_name, FieldableEntityInterface $entity)
 {
     // If the field item class implements the interface, create an orphaned
     // runtime item object, so that it can be used as the options provider
     // without modifying the entity being worked on.
     if (is_subclass_of($this->getFieldItemClass(), '\\Drupal\\Core\\TypedData\\OptionsProviderInterface')) {
         $items = $entity->get($this->getName());
         return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0);
     }
     // @todo: Allow setting custom options provider, see
     // https://www.drupal.org/node/2002138.
 }
 /**
  * {@inheritdoc}
  */
 public function getChangedTime()
 {
     return $this->translation->hasField('content_translation_changed') ? $this->translation->get('content_translation_changed')->value : $this->translation->getChangedTime();
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function getOptionsProvider($property_name, FieldableEntityInterface $entity)
 {
     // If the field item class implements the interface, proxy it through.
     $item = $entity->get($this->getName())->first();
     if ($item instanceof OptionsProviderInterface) {
         return $item;
     }
     // @todo: Allow setting custom options provider, see
     // https://www.drupal.org/node/2002138.
 }