You are here

public static function RssNews::lazyBuild in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Dashboard/RssNews.php \Drupal\dashboards\Plugin\Dashboard\RssNews::lazyBuild()

Lazy builder callback.

Parameters

\Drupal\dashboards\Plugin\DashboardBase $plugin: Plugin id.

array $configuration: Plugin configuration.

Return value

array Rendering array.

Overrides DashboardLazyBuildInterface::lazyBuild

File

src/Plugin/Dashboard/RssNews.php, line 97

Class

RssNews
Show account info.

Namespace

Drupal\dashboards\Plugin\Dashboard

Code

public static function lazyBuild(DashboardBase $plugin, array $configuration) : array {
  $lastAccess = \Drupal::currentUser()
    ->getLastAccessedTime();
  $url = $configuration['uri'];
  $max_items = $configuration['max_items'];
  $show_description = $configuration['show_description'];

  /** @var \Drupal\Core\StringTranslation\TranslationManager $translation_manager */
  $translation_manager = \Drupal::service('string_translation');
  try {
    $items = static::readSource($plugin
      ->getPluginId(), $url);
    if (count($items) > $max_items) {
      $items = array_slice($items, 0, $max_items);
    }
    $links = [];
    foreach ($items as $item) {
      $date = new DrupalDateTime($item['date'], 'UTC');
      $newIndicator = '';
      if ($date
        ->getTimestamp() > $lastAccess) {
        $newIndicator = ' | ' . $translation_manager
          ->translate('New');
      }
      $date = \Drupal::service('date.formatter')
        ->format($date
        ->getTimestamp(), 'short');
      if ($show_description) {
        $links[] = [
          'title' => [
            '#type' => 'inline_template',
            '#template' => '<h6>{{ content }}</h6>',
            '#context' => [
              'date' => [
                '#markup' => $item['date'],
              ],
              'content' => [
                '#type' => 'link',
                '#url' => Url::fromUri($item['link']),
                '#title' => $item['title'],
                '#attributes' => [
                  'target' => '_blank',
                ],
              ],
            ],
          ],
          'date' => [
            '#type' => 'inline_template',
            '#template' => '<p><em>{{ date }} {{ new }}</em></p>',
            '#context' => [
              'date' => [
                '#markup' => $date,
              ],
              'new' => [
                '#markup' => $newIndicator,
              ],
            ],
          ],
          'description' => [
            '#type' => 'inline_template',
            '#template' => '{{ content|raw }}',
            '#context' => [
              'content' => strip_tags($item['description'], '<img> <a> <ul> <li> <p>'),
            ],
          ],
        ];
      }
      else {
        $links[] = [
          '#type' => 'inline_template',
          '#template' => '<h6>{{ content }}</h6> <em>{{ date }} {{ new }}</em>',
          '#context' => [
            'date' => [
              '#markup' => $date,
            ],
            'new' => [
              '#markup' => $newIndicator,
            ],
            'content' => [
              '#type' => 'link',
              '#url' => Url::fromUri($item['link']),
              '#title' => $item['title'],
              '#attributes' => [
                'target' => '_blank',
              ],
            ],
          ],
        ];
      }
    }
    return [
      '#theme' => 'item_list',
      '#items' => $links,
      '#cache' => [
        'max-age' => static::CACHE_TIME,
      ],
    ];
  } catch (\Exception $ex) {
    return [
      '#markup' => $translation_manager
        ->translate('Could not read @url', [
        '@url' => $url,
      ]),
    ];
  }
  return [
    '#markup' => 'here',
  ];
}