You are here

public static function Blazy::containerAttributes in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/Blazy.php \Drupal\blazy\Blazy::containerAttributes()
  2. 8 src/Blazy.php \Drupal\blazy\Blazy::containerAttributes()

Provides container attributes for .blazy container: .field, .view, etc.

Blazy output can be passed as theme_field() or theme_item_list(). At D7, attributes are stored as classes_array and attributes_array. as commonly found at template_preproces_BLAH, unless without preprocess. The $variables here is interchanged as $variables or $attributes for D7.

2 calls to Blazy::containerAttributes()
BlazyGrid::attributes in src/BlazyGrid.php
Provides reusable container attributes.
blazy_preprocess_field in ./blazy.runtime.inc
Implements hook_preprocess_field().

File

src/Blazy.php, line 205

Class

Blazy
Implements BlazyInterface.

Namespace

Drupal\blazy

Code

public static function containerAttributes(array &$variables, array $settings = []) {
  $settings += [
    'namespace' => 'blazy',
  ];
  $is_vars = isset($variables['classes_array']);
  $classes = $is_vars ? $variables['classes_array'] : (empty($variables['class']) ? [] : $variables['class']);
  $attrs['data-blazy'] = empty($settings['blazy_data']) ? '' : drupal_json_encode($settings['blazy_data']);

  // Provides data-LIGHTBOX-gallery to not conflict with original modules.
  if (!empty($settings['media_switch']) && $settings['media_switch'] != 'content') {
    $switch = str_replace('_', '-', $settings['media_switch']);
    $attrs['data-' . $switch . '-gallery'] = '';
    $classes[] = 'blazy--' . $switch;
  }

  // Provides contextual classes relevant to the container: .field, or .view.
  // Sniffs for Views to allow block__no_wrapper, views__no_wrapper, etc.
  foreach ([
    'field',
    'view',
  ] as $key) {
    if (!empty($settings[$key . '_name'])) {
      $name = str_replace('_', '-', $settings[$key . '_name']);
      $name = $key == 'view' ? 'view--' . $name : $name;
      $classes[] = $settings['namespace'] . '--' . $key;
      $classes[] = $settings['namespace'] . '--' . $name;
      if (!empty($settings['current_view_mode'])) {
        $view_mode = str_replace('_', '-', $settings['current_view_mode']);
        $classes[] = $settings['namespace'] . '--' . $name . '--' . $view_mode;
      }
    }
  }

  // While D8 makes themers' life easier, bear with D7 limitations.
  if ($is_vars) {

    // Hence theme_field() expects attributes_array with (pre)process.
    $variables['attributes_array'] = isset($variables['attributes_array']) ? NestedArray::mergeDeep($variables['attributes_array'], $attrs) : $attrs;
  }
  else {

    // Hence theme_item_list() expects attributes without (pre)process.
    $variables = NestedArray::mergeDeep($variables, $attrs);
  }
  $variables[$is_vars ? 'classes_array' : 'class'] = array_merge([
    'blazy',
  ], $classes);
}