/**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $elements = [];
     foreach ($items as $delta => $item) {
         $elements[$delta] = ['#type' => 'markup', '#markup' => $item->value, '#allowed_tags' => _aggregator_allowed_tags()];
     }
     return $elements;
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function buildComponents(array &$build, array $entities, array $displays, $view_mode)
 {
     parent::buildComponents($build, $entities, $displays, $view_mode);
     foreach ($entities as $id => $entity) {
         $bundle = $entity->bundle();
         $display = $displays[$bundle];
         if ($display->getComponent('description')) {
             $build[$id]['description'] = array('#markup' => $entity->getDescription(), '#allowed_tags' => _aggregator_allowed_tags(), '#prefix' => '<div class="item-description">', '#suffix' => '</div>');
         }
     }
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function buildComponents(array &$build, array $entities, array $displays, $view_mode)
 {
     parent::buildComponents($build, $entities, $displays, $view_mode);
     foreach ($entities as $id => $entity) {
         $bundle = $entity->bundle();
         $display = $displays[$bundle];
         if ($display->getComponent('items')) {
             // When in summary view mode, respect the list_max setting.
             $limit = $view_mode == 'summary' ? $this->config->get('source.list_max') : 20;
             // Retrieve the items attached to this feed.
             $items = $this->entityManager->getStorage('aggregator_item')->loadByFeed($entity->id(), $limit);
             $build[$id]['items'] = $this->entityManager->getViewBuilder('aggregator_item')->viewMultiple($items, $view_mode, $entity->language()->getId());
             if ($view_mode == 'full') {
                 // Also add the pager.
                 $build[$id]['pager'] = array('#type' => 'pager');
             }
         }
         if ($display->getComponent('description')) {
             $build[$id]['description'] = array('#markup' => $entity->getDescription(), '#allowed_tags' => _aggregator_allowed_tags(), '#prefix' => '<div class="feed-description">', '#suffix' => '</div>');
         }
         if ($display->getComponent('image')) {
             $image_link = array();
             // Render the image as link if it is available.
             $image = $entity->getImage();
             $label = $entity->label();
             $link_href = $entity->getWebsiteUrl();
             if ($image && $label && $link_href) {
                 $link_title = array('#theme' => 'image', '#uri' => $image, '#alt' => $label);
                 $image_link = array('#type' => 'link', '#title' => $link_title, '#url' => Url::fromUri($link_href), '#options' => array('attributes' => array('class' => array('feed-image'))));
             }
             $build[$id]['image'] = $image_link;
         }
         if ($display->getComponent('feed_icon')) {
             $build[$id]['feed_icon'] = array('#theme' => 'feed_icon', '#url' => $entity->getUrl(), '#title' => t('@title feed', array('@title' => $entity->label())));
         }
         if ($display->getComponent('more_link')) {
             $title_stripped = strip_tags($entity->label());
             $build[$id]['more_link'] = array('#type' => 'link', '#title' => t('More<span class="visually-hidden"> posts about @title</span>', array('@title' => $title_stripped)), '#url' => Url::fromRoute('entity.aggregator_feed.canonical', ['aggregator_feed' => $entity->id()]), '#options' => array('attributes' => array('title' => $title_stripped)));
         }
     }
 }
示例#4
0
 /**
  * Tests basic aggregator_item view.
  */
 public function testAggregatorItemView()
 {
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $feed = $this->feedStorage->create(array('title' => $this->randomMachineName(), 'url' => 'https://www.drupal.org/', 'refresh' => 900, 'checked' => 123543535, 'description' => $this->randomMachineName()));
     $feed->save();
     $items = array();
     $expected = array();
     for ($i = 0; $i < 10; $i++) {
         $values = array();
         $values['fid'] = $feed->id();
         $values['timestamp'] = mt_rand(REQUEST_TIME - 10, REQUEST_TIME + 10);
         $values['title'] = $this->randomMachineName();
         $values['description'] = $this->randomMachineName();
         // Add a image to ensure that the sanitizing can be tested below.
         $values['author'] = $this->randomMachineName() . '<img src="http://example.com/example.png" \\>"';
         $values['link'] = 'https://www.drupal.org/node/' . mt_rand(1000, 10000);
         $values['guid'] = $this->randomString();
         $aggregator_item = $this->itemStorage->create($values);
         $aggregator_item->save();
         $items[$aggregator_item->id()] = $aggregator_item;
         $values['iid'] = $aggregator_item->id();
         $expected[] = $values;
     }
     $view = Views::getView('test_aggregator_items');
     $this->executeView($view);
     $column_map = array('iid' => 'iid', 'title' => 'title', 'aggregator_item_timestamp' => 'timestamp', 'description' => 'description', 'aggregator_item_author' => 'author');
     $this->assertIdenticalResultset($view, $expected, $column_map);
     // Ensure that the rendering of the linked title works as expected.
     foreach ($view->result as $row) {
         $iid = $view->field['iid']->getValue($row);
         $expected_link = \Drupal::l($items[$iid]->getTitle(), Url::fromUri($items[$iid]->getLink(), ['absolute' => TRUE]));
         $output = $renderer->executeInRenderContext(new RenderContext(), function () use($view, $row) {
             return $view->field['title']->advancedRender($row);
         });
         $this->assertEqual($output, $expected_link->getGeneratedLink(), 'Ensure the right link is generated');
         $expected_author = Xss::filter($items[$iid]->getAuthor(), _aggregator_allowed_tags());
         $output = $renderer->executeInRenderContext(new RenderContext(), function () use($view, $row) {
             return $view->field['author']->advancedRender($row);
         });
         $this->assertEqual($output, $expected_author, 'Ensure the author got filtered');
         $expected_description = Xss::filter($items[$iid]->getDescription(), _aggregator_allowed_tags());
         $output = $renderer->executeInRenderContext(new RenderContext(), function () use($view, $row) {
             return $view->field['description']->advancedRender($row);
         });
         $this->assertEqual($output, $expected_description, 'Ensure the author got filtered');
     }
 }