You are here

function instagram_block_block_view in Instagram Block 7

Implements hook_block_view().

Fetches the instagram photos and creates a render array.

File

./instagram_block.module, line 196
Module file for the instagram_block module.

Code

function instagram_block_block_view($delta = '') {

  // Get configuration values.
  $config = variable_get('instagram_block_admin_settings', array());
  $content = array();
  $block = array(
    'subject' => 'Instagram block',
    'content' => array(
      '#attached' => array(
        'css' => array(
          drupal_get_path('module', 'instagram_block') . '/css/instagram-block.css',
        ),
      ),
    ),
  );

  // Check that block configuration is available.
  if (empty($config['access_token'])) {

    // Remind user to fill in configuration.
    $content = instagram_block_get_configuration_reminder();
  }
  else {

    // Build and send request to the Instagram API.
    try {
      switch ($delta) {
        case 'instagram_block':
          $values = variable_get('instagram_block_user_block_settings', array());
          drupal_alter('instagram_block_settings', $delta, $values, $config);
          $request = new InstagramRequest($config, $values);
          $request
            ->requestUserMedia();
          $block['subject'] = t('Instagram Block - User Block');
          break;
        case 'instagram_block_tag':
          $values = variable_get('instagram_block_tag_block_settings', array());
          drupal_alter('instagram_block_settings', $delta, $values, $config);
          $request = new InstagramRequest($config, $values);
          $request
            ->requestTagMedia();
          $block['subject'] = t('Instagram Block - Tag Block');
          break;
      }
    } catch (Exception $e) {
      watchdog_exception('instagram_block', $e);

      // Return no content, the request failed.
      return $block;
    }

    // Set resolution from block config.
    $img_resolution = isset($values['img_resolution']) ? $values['img_resolution'] : 'thumbnail';
    $response = $request
      ->get_instagram_posts();
    foreach ($response as $post) {
      $url = $post->images->{$img_resolution}->url;
      $components = parse_url($url);
      $content['children'][] = array(
        '#markup' => '',
        '#theme' => 'instagram_block_image',
        '#post' => $post,
        '#href' => $post->link,
        '#src' => str_replace($components['scheme'] . '://', '//', $url),
        '#width' => $values['width'],
        '#height' => $values['height'],
      );
    }
  }
  $block['content']['#markup'] = theme('instagram_block', array(
    'content' => $content,
    'response' => isset($response) ? $response : NULL,
  ));
  return $block;
}