Commit 30b795c8 authored by Ryan Jacobs's avatar Ryan Jacobs
Browse files

Issue #2217791 and Issue #2275051 by rjacobs: Port multiple fixes from 7.x-2.x...

Issue #2217791 and Issue #2275051 by rjacobs: Port multiple fixes from 7.x-2.x to 8.x-2.x and catch-up on some common refactoring across branches.
parent 98b01568
Loading
Loading
Loading
Loading
+4 −2
Changes for juicebox.module: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@

use Drupal\Component\Utility\String;
use Drupal\Core\Render\Element;
use Drupal\juicebox\JuiceboxGalleryInterface;
use Drupal\juicebox\JuiceboxGalleryDrupalInterface;

/**
 * Instantiate a Juicebox object along with its dependencies.
@@ -23,9 +25,9 @@ function juicebox() {
  drupal_alter('juicebox_classes', $classes, $library);
  // Instantiate the Juicebox gallery objects.
  $gallery = new $classes['gallery']();
  if (in_array('Drupal\juicebox\JuiceboxGalleryInterface', class_implements($gallery))) {
  if ($gallery instanceof JuiceboxGalleryInterface) {
    $juicebox = new $classes['juicebox']($gallery, $library, \Drupal::configFactory(),  \Drupal::translation(), \Drupal::request());
    if (in_array('Drupal\juicebox\JuiceboxGalleryDrupalInterface', class_implements($juicebox))) {
    if ($juicebox instanceof JuiceboxGalleryDrupalInterface) {
      return $juicebox;
    }
  }
+9 −9
Changes for juicebox.routing.yml: 9 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -8,15 +8,15 @@ juicebox.xml_field:
    _access: 'TRUE'
    args: '^field/([0-9a-zA-Z_\/-])+'
    
juicebox.xml_viewsstyle:
  path: '/juicebox/xml/{args}'
  defaults:
    _content: '\Drupal\juicebox\Controller\JuiceboxXmlControllerViewsStyle::xmlController'
    # use the option below if the function should just print raw text (XML)
    #_controller: '\Drupal\juicebox\Controller\JuiceboxXmlController::getXml'
  requirements:
    _access: 'TRUE'
    args: '^viewsstyle/([0-9a-zA-Z_\/-])+'
# juicebox.xml_viewsstyle:
#   path: '/juicebox/xml/{args}'
#   defaults:
#     _content: '\Drupal\juicebox\Controller\JuiceboxXmlControllerViewsStyle::xmlController'
#     # use the option below if the function should just print raw text (XML)
#     #_controller: '\Drupal\juicebox\Controller\JuiceboxXmlController::getXml'
#   requirements:
#     _access: 'TRUE'
#     args: '^viewsstyle/([0-9a-zA-Z_\/-])+'
    
juicebox.admin_settings:
  path: '/admin/config/media/juicebox'
+8 −3
Changes for src/Controller/JuiceboxXmlControllerBase.php: 8 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -20,18 +20,22 @@ use Drupal\Core\Config\ConfigFactoryInterface;
abstract class JuiceboxXmlControllerBase implements ContainerInjectionInterface {

  protected $configFactory;
  protected $settings = array();

  /**
   * Constructor
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
    // Fetch and store the Juicebox-specific global settings.
    $this->settings = $config_factory->get('juicebox.settings')->get();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    // Create the actual controller instance.
    return new static($container->get('config.factory'));
  }

@@ -56,9 +60,10 @@ abstract class JuiceboxXmlControllerBase implements ContainerInjectionInterface
      $xml = $this->getXml();
      // Package the XML as a Response object.
      $xml_obj = new Response($xml);
      // Set headers.
      $xml_obj->headers->set('Content-Type', 'text/xml');
      if ($this->configFactory->get('juicebox.settings')->get('enable_cors')) {
      // Set headers that apply only to XML requests.
      $xml_obj->headers->set('Content-Type', 'application/xml; charset=utf-8');
      $xml_obj->headers->set('X-Robots-Tag', 'noindex');
      if ($this->settings['enable_cors']) {
        $xml_obj->headers->set('Access-Control-Allow-Origin', '*');
      }
      return $xml_obj;
+1 −1
Changes for src/Form/SettingsForm.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ class SettingsForm extends ConfigFormBase {
      '#type' => 'checkbox',
      '#title' => t('Allow galleries to be embedded remotely (CORS support)'),
      '#default_value' => $settings['enable_cors'],
      '#description' => t('Enable cross-origin resource sharing (CORS) for all generated Juicebox XML. This will allow all origins/domains to use Juicebox XML requested from this site for embedding purposes.'),
      '#description' => t('Enable cross-origin resource sharing (CORS) for all generated Juicebox XML. This will allow all origins/domains to use any Juicebox XML requested from this site for embedding purposes (adds a <em>Access-Control-Allow-Origin: *</em> header to all Juicebox XML responses).'),
    );
    $form['multilingual'] = array(
      '#type' => 'details',
+46 −58
Changes for src/JuiceboxGalleryDrupal.php: 46 added lines, 58 removed lines.
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ use Drupal\juicebox\JuiceboxGalleryDrupalInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Unicode;
use Drupal\file\FileInterface;

/**
@@ -26,7 +27,6 @@ class JuiceboxGalleryDrupal extends JuiceboxGalleryDecorator implements Juicebox
  // Base injected properties.
  protected $library = array();
  protected $configFactory;
  protected $globalSettings = array();
  protected $stringTranslation;
  protected $request;
  // Properties set only upon initializing into a specific gallery.
@@ -56,8 +56,6 @@ class JuiceboxGalleryDrupal extends JuiceboxGalleryDecorator implements Juicebox
    $this->gallery = $gallery;
    $this->library = $library;
    $this->configFactory = $config_factory;
    // Fetch and store the Juicebox-specific global settings.
    $this->globalSettings = $config_factory->get('juicebox.settings');
    $this->stringTranslation = $translation;
    $this->request = $request;
  }
@@ -68,7 +66,8 @@ class JuiceboxGalleryDrupal extends JuiceboxGalleryDecorator implements Juicebox
  public function init($id_args, $settings = array(), $data = NULL) {
    // Set path for reference.
    $this->idArgs = $id_args;
    $this->settings = $settings;
    // Merge local and global settings.
    $this->settings = $settings + $this->configFactory->get('juicebox.settings')->get();
    $this->data = $data;
    // Calculate and set the gallery ID.
    $id = '';
@@ -81,7 +80,6 @@ class JuiceboxGalleryDrupal extends JuiceboxGalleryDecorator implements Juicebox
    $this->initialized = TRUE;
  }


  /**
   * {@inheritdoc}
   */
@@ -113,8 +111,8 @@ class JuiceboxGalleryDrupal extends JuiceboxGalleryDecorator implements Juicebox
    // Add all gallery options directly from the settings.
    $this->setGalleryOptions();
    // Also attempt to translate interface strings.
    if ($this->globalSettings->get('translate_interface')) {
      $base_string = $this->globalSettings->get('base_languagelist');
    if ($this->settings['translate_interface']) {
      $base_string = $this->settings['base_languagelist'];
      if (!empty($base_string)) {
        $this->addOption('languagelist', String::checkPlain($this->stringTranslation->translate($base_string)), FALSE);
      }
@@ -127,72 +125,62 @@ class JuiceboxGalleryDrupal extends JuiceboxGalleryDecorator implements Juicebox
  /**
   * {@inheritdoc}
   */
  public function getEmbed($xml_path = '') {
    $settings = $this->settings;
  public function buildEmbed($xml_path = '', $add_js = TRUE, $context = array()) {
    // Capture the current query parameters and a checksum value in the XML.
    $current_query = $this->request->query->all();
    $xml_query = array_merge(array('checksum' => $this->getChecksum()), $current_query);
    // Maintain XML details as structured data because they may need further
    // preprocessing.
    $xml = array('path' => $xml_path, 'options' => array('query' => $xml_query));
    // If debugging links are allowed and "jb-debug" is active in the query we
    // display debug info in place of the actual gallery.
    $settings['show_debug_links'] = $this->globalSettings->get('show_debug_links');
    if ($settings['show_debug_links'] && array_key_exists('jb-debug', $current_query)) {
      // @todo: Build render array using theme system.
      return array();
    }
    // The returned render array will contain some common values no matter what
    // output we produce (normal and debug).
    $output = array(
      // @todo: add c_links once theme layer is ready
      // '#c_links' => $this->buildContextualLinks($xml_path, $context),
      '#gallery_id' => $this->getId(),
      '#gallery_images' => $this->getImages(),
      '#gallery_options' => $this->getOptions(),
      '#xml' => $xml,
      '#settings' => $this->settings,
      '#juicebox' => $this
    );
    // If "jb-debug" is active in the query we display debug info in place of
    // the actual gallery.
    if (array_key_exists('jb-debug', $current_query) && $this->accessDebugDisplay()) {
      // @todo: Enable #theme once theme layer is ready
      // $output['#theme'] = 'juicebox_debug_markup';
      $output['#attached']['css'] = array(drupal_get_path('module', 'juicebox') . '/css/admin_styles.css');
    }
    // Otherwise we are just displaying the normal gallery.
    else {
      // Construct a render array for the embed markup.
      $element = array(
        '#markup' => $this->renderEmbed(), // @todo: Switch to theme function.
        '#attached' => $this->getEmbedAttached(url($xml['path'], $xml['options'])),
        '#juicebox' => $this,
      // @todo: Enable #theme once theme layer is ready
      // $output['#theme'] = 'juicebox_embed_markup';
      if ($add_js) {
        // @todo: maybe replace url() with urlGenerator service.
        $xml_url = url($xml['path'], $xml['options']);
        $output['#attached']['libraries_load'][] = array('juicebox');
        $output['#attached']['js'][] = array(
          'data' => $this->renderJavascript($xml_url),
          'type' => 'inline',
          'scope' => $this->settings['js_scope'],
          'weight' => 10, // load below main library
        );
      return $element;
      }
    }
    // @todo: Remove the line below once theme layer is ready
    $output['#markup'] = $this->renderEmbed();
    return $output;
  }

  /**
   * {@inheritdoc}
   */
  public function addImage($src_data = array(), $title = '', $caption = '', $filter = TRUE, $override_id = NULL, $offset = NULL) {
    // We should always get the filter setting from global variables.
    $filter = $this->globalSettings->get('apply_markup_filter');
    $filter = $this->settings['apply_markup_filter'];
    return $this->gallery->addImage($src_data, $title, $caption, $filter, $override_id, $offset);
  }

  /**
   * Helper to get the "asset" info for a gallery in a form that can be used in
   * the '#attached' part of a render array.
   *
   * @param string $xml_url
   *   A full URL for the gallery XML.
   * @return array
   *   An associative array of info that can be used directly as the '#attached'
   *   value in a render array for the gallery.
   */
  protected function getEmbedAttached($xml_url) {
    $attached = array();
    // Get the library specifics as this is where the main library javascript
    // details are defined.
    $library = juicebox_library_detect();
    // See if the $library array has the data needed.
    if (!empty($library['installed']) && !empty($library['files']) && !empty($library['library path'])) {
      // Adjust the 'files' info to use the full Drupal path of each file as a
      // key.
      foreach ($library['files'] as $type => $file) {
        foreach ($file as $filename => $options) {
          $filepath = $library['library path'] . '/' . $filename;
          $attached[$type][] = array('data' => $filepath, 'type' => 'file') + $options;
        }
      }
      // Add-in the gallery-specific javascript.
      $attached['js'][] = array('data' => $this->renderJavascript($xml_url), 'type' => 'inline', 'scope' => $this->globalSettings->get('js_scope'), 'weight' => 10);
    }
    return $attached;
  }

  /**
   * {@inheritdoc}
   */
@@ -286,14 +274,14 @@ class JuiceboxGalleryDrupal extends JuiceboxGalleryDecorator implements Juicebox
    foreach (array('jlib_galleryWidth', 'jlib_galleryHeight', 'jlib_backgroundColor', 'jlib_textColor', 'jlib_thumbFrameColor') as $name) {
      if (isset($settings[$name])) {
        $name_real = str_replace('jlib_', '', $name);
        $this->addOption(drupal_strtolower($name_real), trim(String::checkPlain($settings[$name])));
        $this->addOption(Unicode::strtolower($name_real), trim(String::checkPlain($settings[$name])));
      }
    }
    // Get the bool options set via the GUI.
    foreach (array('jlib_showOpenButton', 'jlib_showExpandButton', 'jlib_showThumbsButton', 'jlib_useThumbDots', 'jlib_useFullscreenExpand') as $name) {
      if (isset($settings[$name])) {
        $name_real = str_replace('jlib_', '', $name);
        $this->addOption(drupal_strtolower($name_real), (!empty($settings[$name]) ? 'TRUE' : 'FALSE'));
        $this->addOption(Unicode::strtolower($name_real), (!empty($settings[$name]) ? 'TRUE' : 'FALSE'));
      }
    }
    // Merge-in the manually assigned options making sure they take priority
@@ -309,8 +297,8 @@ class JuiceboxGalleryDrupal extends JuiceboxGalleryDecorator implements Juicebox
          $matches = array();
          preg_match('/^([A-Za-z0-9]+?)="([^"]+?)"$/u', $option, $matches);
          list($full_match, $name, $value) = $matches;
          $name = drupal_strtolower($name);
          $this->addOption(drupal_strtolower($name), String::checkPlain($value));
          $name = Unicode::strtolower($name);
          $this->addOption(Unicode::strtolower($name), String::checkPlain($value));
        }
      }
    }
Loading