You are here

public function JuiceboxFormatter::newGallery in Juicebox HTML5 Responsive Image Galleries 8.2

Same name and namespace in other branches
  1. 8.3 src/JuiceboxFormatter.php \Drupal\juicebox\JuiceboxFormatter::newGallery()

Create and initialize a new Juicebox gallery object.

Parameters

array $id_args: An indexed array of simple string arguments that describe this gallery. This is typically based on the arguments that will be used to create a URL for the gallery XML, but no formal structure is strictly required. This information should uniquely identify the gallery.

Return value

Drupal\juicebox\JuiceboxGalleryInterface An initialized Juicebox gallery object.

Overrides JuiceboxFormatterInterface::newGallery

File

src/JuiceboxFormatter.php, line 100

Class

JuiceboxFormatter
Class to define a Drupal service with common formatter methods.

Namespace

Drupal\juicebox

Code

public function newGallery(array $id_args) {

  // Calculate the gallery ID.
  $id = '';
  foreach ($id_args as $arg) {

    // Drop special characters in individual args and delimit by double-dash.
    $arg = preg_replace('/[^0-9a-zA-Z-]/', '-', $arg);
    $id .= $arg . '--';
  }
  $id = trim($id, '- ');

  // Get the library data. We do this early (before instantiating) as the lib
  // details should be allowed to impact which classes are used.
  $library = $this
    ->getLibrary();

  // Calculate the class that needs to be instantiated allowing modules to
  // alter the result.
  $class = 'Drupal\\juicebox\\JuiceboxGallery';
  $this->moduleManager
    ->alter('juicebox_gallery_class', $class, $library);

  // Instantiate the Juicebox gallery objects.
  $object_settings = [
    'filter_markup' => $this->configFactory
      ->get('juicebox.settings')
      ->get('apply_markup_filter'),
    'process_attributes' => FALSE,
  ];
  $gallery = new $class($id, $object_settings);
  if ($gallery instanceof JuiceboxGalleryInterface) {
    return $gallery;
  }
  throw new \Exception(t('Cound not instantiate Juicebox gallery.'));
}