You are here

protected function JuiceboxGallery::processAttributes in Juicebox HTML5 Responsive Image Galleries 8.3

Same name and namespace in other branches
  1. 8.2 src/JuiceboxGallery.php \Drupal\juicebox\JuiceboxGallery::processAttributes()

Process an attribute list for valid use in a Juicebox gallery.

For legacy support we can allow a variety of attribute name inputs, including underscore-separated and dash-separated values. Because the Juicebox library expects attribute names in camelCase we use this method to make a best-effort conversion (e.g., convert image_url to imageURL). Note that this process does NOT do any sanitization and can only correctly convert certain input names.

Parameters

array $attributes: An associative array of name => value pairs for juicebox XML attributes to be converted.

Return value

array The converted array of attributes.

3 calls to JuiceboxGallery::processAttributes()
JuiceboxGallery::addImage in src/JuiceboxGallery.php
Add a new image to the gallery.
JuiceboxGallery::getImages in src/JuiceboxGallery.php
Getter method for the gallery images.
JuiceboxGallery::getOptions in src/JuiceboxGallery.php
Getter method for the gallery options.

File

src/JuiceboxGallery.php, line 325

Class

JuiceboxGallery
Class to generate the script and markup for a Juicebox gallery.

Namespace

Drupal\juicebox

Code

protected function processAttributes(array $attributes) {
  $filtered = [];
  foreach ($attributes as $name => $value) {

    // First make some adjustments for legacy support. We used to use some
    // specialized keys that are no longer valid, but still want to support
    // them as input. These values are special and cannot be handled alone by
    // the word-separator processing logic below.
    $name_mappings = [
      'image_url_small' => 'smallImageURL',
      'image_url_large' => 'largeImageURL',
    ];
    if (array_key_exists($name, $name_mappings)) {
      $name = $name_mappings[$name];
    }

    // Check that the name is in a format expected by the XML such that any
    // word-separators (like "-" and "_") are dropped. Also, the Juicebox
    // library uses camelCase for attributes so we do our best to deliver
    // that. Note that for image attributes proper camelCase appears to be
    // REQUIRED, but for gallery attributes all-lowercase values will work in
    // addition to camelCase.
    $parts = preg_split('/(_|-)/', $name);
    $i = 0;
    foreach ($parts as &$word) {

      // Don't alter the first word.
      if ($i) {
        $word = ucfirst($word);

        // For some reason the library requires that some attributes
        // containing a "url" string capitalize the "url" part.
        $word = $word == 'Url' ? 'URL' : $word;
      }
      $i++;
    }
    $name = implode('', $parts);
    $filtered[$name] = $value;
  }
  return $filtered;
}