You are here

public function SimpleInstagramBlock::build in Simple Instagram Feed Block 1.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Block/SimpleInstagramBlock.php \Drupal\simple_instagram_feed\Plugin\Block\SimpleInstagramBlock::build()
  2. 8 src/Plugin/Block/SimpleInstagramBlock.php \Drupal\simple_instagram_feed\Plugin\Block\SimpleInstagramBlock::build()
  3. 8.2 src/Plugin/Block/SimpleInstagramBlock.php \Drupal\simple_instagram_feed\Plugin\Block\SimpleInstagramBlock::build()

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/SimpleInstagramBlock.php, line 222

Class

SimpleInstagramBlock
Provides a block with a dynamic Instagram Feed.

Namespace

Drupal\simple_instagram_feed\Plugin\Block

Code

public function build() {
  $unique_id = Html::getUniqueId($this
    ->getPluginId());

  // TODO: I'm pretty sure this is not being cached.
  // Acquire and cache json from Instagram.
  $instagram_feed_array = $this
    ->get_instagram_json($this->configuration['simple_instagram_username']);

  //Instagram Feed data.
  $instagram_username = $instagram_feed_array['entry_data']['ProfilePage'][0]['graphql']['user']['username'];
  $instagram_fullname = $instagram_feed_array['entry_data']['ProfilePage'][0]['graphql']['user']['full_name'];
  $instagram_bio = $instagram_feed_array['entry_data']['ProfilePage'][0]['graphql']['user']['biography'];
  $profile_pic_url = $instagram_feed_array['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url'];
  $profile_pic_url_hd = $instagram_feed_array['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url_hd'];

  // This contains the array with links to images, descriptive text & video.
  // TODO: Cache the images in the directory at ://public/simple_instagram_feed
  $instagram_media_array = $instagram_feed_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'];
  $build = [
    '#unique_id' => $unique_id,
    '#theme' => 'simple_instagram_block',
    '#markup' => $this
      ->t('Simple Instagram Feed'),
    '#attached' => [
      'library' => [
        'simple_instagram_feed/simple_instagram_block',
      ],
      'drupalSettings' => [],
    ],
    '#instagram_username' => $instagram_username,
    '#instagram_fullname' => $instagram_fullname,
    '#instagram_bio' => $instagram_bio,
    '#profile_pic_url' => $profile_pic_url,
    '#cache' => [
      'max-age' => 43200,
    ],
  ];

  // TODO: Review and correct as necessary
  $build['#attached']['drupalSettings']['simple_instagram_feed'][$unique_id] = $this
    ->buildAttachedSettings();
  $build['#attached']['drupalSettings']['simple_instagram_feed'][$unique_id]['unique_id'] = $unique_id;
  return $build;
}