You are here

function backstretch_context_reaction_backstretch::execute in Backstretch 7.2

Same name and namespace in other branches
  1. 7 plugins/backstretch_context_reaction_backstretch.inc \backstretch_context_reaction_backstretch::execute()

File

plugins/backstretch_context_reaction_backstretch.inc, line 284
Backstretch reaction class for Context integration.

Class

backstretch_context_reaction_backstretch
@file Backstretch reaction class for Context integration.

Code

function execute() {
  foreach ($this
    ->get_contexts() as $context) {
    if (!empty($context->reactions[$this->plugin])) {

      // We need an unique id for every Backstretch.
      static $id = 1;

      // Getting context settings.
      $settings = $context->reactions[$this->plugin];

      // Store the source stuff here.
      $source = $settings['source']['type'];

      // We store the default values from Backstretch formatter here.
      $formatters = backstretch_field_formatter_info();
      $default = $formatters['backstretch']['settings'];

      // We need the js variable name.
      $options_info = backstretch_formatter_options();

      // Here we store all options later.
      $options = array();
      foreach ($settings as $name => $value) {
        if (array_key_exists($name, $options_info)) {
          $option = $options_info[$name];
          $js = isset($option['js']) ? $option['js'] : '';

          // We need some special handling with the element settings.
          if ($name == 'element' || $name == 'element_other' && $settings['element'] == '') {
            continue;
          }

          /*
                       * We have to figure out which tokens should be supported because in
                       * the Context reaction you can also use a path to the image so let
                       * us discuss this in https://drupal.org/node/2140009.

                      // Replace tokens in the "Other element" field.
                      if ($name == 'element_other') {
                        $value = token_replace($value, array($entity_type => $entity));
                      }*/

          // We only put the setting into $options when it is
          // not the default value.
          if ($value != $default[$name] && $js) {
            $options[$js] = $value;
          }

          // The fade option has to be an integer otherwise it doesn't work.
          if ($name == 'fade' && $value != $default[$name] && is_numeric($value) && intval($value) == $value) {
            $options[$name] = (int) $options[$name];
          }
        }
      }

      // We need special handling for every source type.
      switch ($source) {
        case 'entity':

          // We store the entity info.
          $entity_type = $settings['source']['entity']['entity_type'];
          $field = isset($settings['source']['entity']['field']) ? $settings['source']['entity']['field'] : '';
          $entity_ids = $settings['source']['entity']['ids'];
          $entity_ids = explode(',', $entity_ids);

          // We cannot continue without an entity type and an entity ID.
          if (!$entity_type || !$entity_ids) {
            return;
          }
          $entities = entity_load($entity_type, $entity_ids);
          foreach ($entities as $entity) {

            // If there is a file ID we can guess that we have a file here.
            if (isset($entity->fid) && isset($entity->uri)) {
              $options['items'][] = file_create_url($entity->uri);
              continue;
            }

            // Getting the field items.
            $items = field_get_items($entity_type, $entity, $field);

            // Iterate all items and store the absolute url to it.
            foreach ($items as &$item) {
              $uri = $item['uri'];

              // Get url to image styled image if a image style was set.
              if ($settings['image_style']) {
                $url = image_style_url($settings['image_style'], $uri);
              }
              else {
                $url = file_create_url($uri);
              }
              $options['items'][] = $url;
            }
          }
          break;
        case 'path':

          // Just storing the path info.
          $paths = $settings['source']['path'];

          // We need an array for processing the paths.
          $paths = explode("\n", $paths);
          foreach ($paths as $key => $path) {
            $path = trim($path);

            // Just build an url for the path.
            $paths[$key] = url($path);

            // If an image style was set we try to image style the image.
            if ($settings['image_style']) {
              $uri = file_build_uri(drupal_basename($path));
              $paths[$key] = image_style_url($settings['image_style'], $uri);
            }
          }
          $options['items'] = $paths;
          break;
      }
      if ($settings['random']) {
        $random_key = array_rand($options['items']);
        $options['items'] = array(
          $options['items'][$random_key],
        );
      }

      // Prepare a renderable array.
      $element = array(
        '#theme' => 'backstretch',
        '#id' => 'backstretch-' . $id,
        '#options' => $options,
      );

      // Increasing the id.
      $id++;

      // Return the rendered element.
      return drupal_render($element);
    }
  }
}