You are here

instagram_block.module in Instagram Block 7

Module file for the instagram_block module.

File

instagram_block.module
View source
<?php

/**
 * @file
 * Module file for the instagram_block module.
 */

/**
 * Implements hook_help().
 */
function instagram_block_help($path, $arg) {
  switch ($path) {
    case 'admin/help#instagram_block':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Instagram Block module a is a very simple module that integrates with Instagram and creates a block containing your most recent Instagram posts. The block\'s configuration page lets you choose how many posts and what size they should appear in the block. The images are individually exposed to the drupal theme layer, so developers have access to an all of the variables provided by the Instagram api should they choose to extent the block. For more information, see the <a href="!online">online documentation</a> for the Instagram Block module.', array(
        '!online' => 'https://www.drupal.org/node/2727025',
      )) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dd>' . t('To begin using Instagram Block there are 2 things that need configuring, the connection to instagram and the settings for the block. For step by step instructions see the <a href="!online">online documentation</a> for the Instagram Block module.', array(
        '!online' => 'https://www.drupal.org/node/2727025',
      )) . '</dd>';
      $output .= '<dt>' . t('Connecting to Instagram') . '</dt>';
      $output .= '<dd>' . t('Instagram Block requires connecting to a specific Instagram account. You need to be able to log into that account when asked to. The <a href="!help">Authenticate with Instagram</a> page helps with the setup.', array(
        '!help' => 'https://www.drupal.org/node/2746185',
      )) . '</dd>';
      $output .= '<dt>' . t('Configuring block settings') . '</dt>';
      $output .= '<dd>' . t('The Instagram Block page is located at admin/structure/block and provides for configuration of an Instagram block in a similar way to those of other blocks on your site.  This page makes it easy for you to configure the size and number of images that you want to show from your Instagram account.') . '</dd>';
      $output .= '</dl>';
      return $output;
      break;
    case 'admin/config/services/instagram_block':
      $output = '';
      $output .= '<h3>' . t('Connecting to Instagram') . '</h3>';
      $output .= '<dl>';
      $output .= '<dd>' . t('Instagram Block requires connecting to a specific Instagram account. You need to be able to log into that account when asked to. The <a href="!help">Authenticate with Instagram</a> page helps with the setup.', array(
        '!help' => 'https://www.drupal.org/node/2746185',
      )) . '</dd>';
      $output .= '</dl>';
      return $output;
      break;
  }
}

/**
 * Implements hook_block_info().
 *
 * Defines the new instagram block.
 */
function instagram_block_block_info() {

  // @todo Find a better way to manage access for tag block.
  $tag_block = variable_get('instagram_block_enable_tag_block', FALSE);
  $block_config = variable_get('instagram_block_user_block_settings', array());
  $blocks['instagram_block'] = array(
    'info' => t('Instagram Block - User Block'),
    'cache' => isset($block_config['block_cache']) ? $block_config['block_cache'] : DRUPAL_CACHE_GLOBAL,
  );
  if ($tag_block) {
    $tag_block_config = variable_get('instagram_block_tag_block_settings', array());
    $blocks['instagram_block_tag'] = array(
      'info' => t('Instagram Block - Tag Block'),
      'cache' => isset($tag_block_config['block_cache']) ? $tag_block_config['block_cache'] : DRUPAL_CACHE_GLOBAL,
    );
  }
  return $blocks;
}

/**
 * Implements hook_block_configure().
 *
 * Set values to be used when rendering the block later.
 */
function instagram_block_block_configure($delta = '') {
  $form = array();
  $description = '';

  // Default values
  $empty = array(
    'access_token' => '',
  );

  // Store data from variable in $form for now.
  $form['#admin_data'] = variable_get('instagram_block_admin_settings', $empty);
  $form['description'] = array(
    '#title' => t('Description'),
    '#type' => 'item',
    '#markup' => '',
    '#weight' => -50,
  );
  $form['count'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of images to display.'),
  );
  $form['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Image width in pixels.'),
  );
  $form['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Image height in pixels.'),
  );
  $image_options = array(
    'thumbnail' => t('Thumbnail Preview'),
    'low_resolution' => t('Low Resolution'),
    'standard_resolution' => t('Standard Resolution'),
  );
  $form['img_resolution'] = array(
    '#type' => 'select',
    '#title' => t('Image resolution'),
    '#description' => t('Choose the quality of the images you would like to display.'),
    '#options' => $image_options,
  );
  $form['block_cache'] = array(
    '#type' => 'select',
    '#title' => t('Block Cache Settings'),
    '#description' => t('Configure block caching.'),
    '#options' => array(
      DRUPAL_CACHE_PER_ROLE => t('Cache block per role'),
      DRUPAL_CACHE_PER_USER => t('Cache block per user'),
      DRUPAL_CACHE_PER_PAGE => t('Cache block per page'),
      DRUPAL_CACHE_GLOBAL => t('Cache block globally'),
      DRUPAL_NO_CACHE => t('Don\'t cache block'),
    ),
  );
  switch ($delta) {
    case 'instagram_block':

      // Store data from variable in $form for now.
      $form['#block_data'] = variable_get('instagram_block_user_block_settings', array());
      $description = t('Configurations in this block utilise the !globalconfig.', array(
        '!globalconfig' => l('global configuration', 'admin/config/services/instagram_block'),
      ));
      $form['count']['#default_value'] = isset($form['#block_data']['count']) ? $form['#block_data']['count'] : '';
      $form['width']['#default_value'] = isset($form['#block_data']['width']) ? $form['#block_data']['width'] : '';
      $form['height']['#default_value'] = isset($form['#block_data']['height']) ? $form['#block_data']['height'] : '';
      $form['img_resolution']['#default_value'] = isset($form['#block_data']['img_resolution']) ? $form['#block_data']['img_resolution'] : '';
      $form['block_cache']['#default_value'] = isset($form['#block_data']['block_cache']) ? $form['#block_data']['block_cache'] : DRUPAL_CACHE_GLOBAL;
      break;
    case 'instagram_block_tag':
      $form['#block_data'] = variable_get('instagram_block_tag_block_settings', array());
      $description = t('This block won\'t fetch media from your personal instagram account. Configurations in this block utilise the !globalconfig.', array(
        '!globalconfig' => l('global configuration', 'admin/config/services/instagram_block'),
      ));
      $form['count']['#default_value'] = isset($form['#block_data']['count']) ? $form['#block_data']['count'] : '';
      $form['width']['#default_value'] = isset($form['#block_data']['width']) ? $form['#block_data']['width'] : '';
      $form['height']['#default_value'] = isset($form['#block_data']['height']) ? $form['#block_data']['height'] : '';
      $form['img_resolution']['#default_value'] = isset($form['#block_data']['img_resolution']) ? $form['#block_data']['img_resolution'] : '';
      $form['block_cache']['#default_value'] = isset($form['#block_data']['block_cache']) ? $form['#block_data']['block_cache'] : DRUPAL_CACHE_GLOBAL;
      $form['tag'] = array(
        '#type' => 'textfield',
        '#title' => t('Tag'),
        '#description' => t('Fetch media with this tag'),
        '#default_value' => isset($form['#block_data']['tag']) ? $form['#block_data']['tag'] : '',
        '#weight' => -49,
      );
      break;
  }
  $form['description']['#markup'] = $description;
  return $form;
}

/**
 * Implements hook_block_save().
 *
 * Save the information from the configuration form to a drupal variable.
 */
function instagram_block_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case 'instagram_block':
      $variables = variable_get('instagram_block_user_block_settings', array());
      $save_values = array_merge($variables, $edit);
      variable_set('instagram_block_user_block_settings', $save_values);
      break;
    case 'instagram_block_tag':
      $variables = variable_get('instagram_block_tag_block_settings', array());
      $save_values = array_merge($variables, $edit);
      variable_set('instagram_block_tag_block_settings', $save_values);
      break;
  }
}

/**
 * Implements hook_block_view().
 *
 * Fetches the instagram photos and creates a render array.
 */
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;
}

/**
 * Implements hook_menu().
 */
function instagram_block_menu() {
  $items['admin/config/services/instagram_block'] = array(
    'title' => 'Instagram Block',
    'description' => 'Configuration page for Instagram Block.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'instagram_block_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'instagram_block.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function instagram_block_theme() {
  return array(
    'instagram_block_image' => array(
      'variables' => array(
        'height' => NULL,
        'width' => NULL,
        'src' => NULL,
        'href' => NULL,
        'post' => NULL,
      ),
      'template' => 'instagram-block-image',
    ),
    'instagram_block' => array(
      'variables' => array(
        'content' => NULL,
        'response' => NULL,
      ),
      'template' => 'instagram-block',
    ),
  );
}

/**
 * Builds render array to remind user to configure block settings.
 */
function instagram_block_get_configuration_reminder() {
  return array(
    '#markup' => t('Please don\'t forget to !configure this block.', array(
      '!configure' => l(t('configure'), 'admin/config/services/instagram_block'),
    )),
  );
}

/**
 * Preprocess variables for instagram-block-image.tpl.php
 *
 * Provides a default caption to be used as the alt text for an img tag.
 *
 * @see instagram-block-image.tpl.php
 */
function template_preprocess_instagram_block_image(&$variables) {
  if (!empty($variables['post']->caption->text)) {
    $variables['caption'] = check_plain($variables['post']->caption->text);
  }
  else {
    $variables['caption'] = t('Instagram image, no caption provided by author.');
  }
}