You are here

public function FavBlock::build in Favorites 8.2

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/FavBlock.php, line 35
Contains Drupal\favorites\Plugin\Block\FavBlock.

Class

FavBlock
Provides a 'favorites' block.

Namespace

Drupal\favorites\Plugin\Block

Code

public function build() {
  global $base_url;
  $form = \Drupal::formBuilder()
    ->getForm('Drupal\\favorites\\Form\\AddForm');
  $items = array();
  $account = \Drupal::currentUser();
  $uid = $account
    ->id();
  if ($uid) {
    $result = FavoriteStorage::getFavorites($uid);
    $i = 0;
    foreach ($result as $favorite) {
      $favorite->path = \Drupal::service('path.alias_manager')
        ->getAliasByPath('/' . trim($favorite->path, '/'));
      if ($favorite->query != '') {
        $url = $base_url . $favorite->path . '?' . $favorite->query;
      }
      else {
        $url = $base_url . $favorite->path;
      }
      $url = Url::fromUri($url);
      $url_delete = Url::fromRoute('favorites.remove', [
        'fid' => $favorite->fid,
      ]);
      $items[$i]['title_link'] = \Drupal::l($favorite->title, $url);
      $items[$i]['remove_link'] = \Drupal::l('X', $url_delete);
      $items[$i]['id'] = $favorite->fid;
      $i++;
    }
  }
  return array(
    '#attached' => array(
      'library' => array(
        'favorites/favorites.custom',
      ),
    ),
    'fav_lists' => array(
      '#theme' => 'favlist_item_list',
      'items' => $items,
    ),
    'add_this_page' => $form,
  );
}