function template_preprocess_isotope_grid in Isotope (with Masonry and Packery) 8
Prepares variables for isotope grid template.
Parameters
array $variables: An associative array of variables.
File
- ./
isotope.theme.inc, line 20 - Preprocessors of isotope module.
Code
function template_preprocess_isotope_grid(&$variables) {
$presentation = Presentation::load($variables['presentation']);
// Assemble list of libraries to attach.
$plugins = [
$presentation->layout_mode,
];
$plugins = array_merge($plugins, array_values($presentation->plugins));
$libraries = array_values(IsotopeController::libraryMapping($plugins));
$libraries[] = 'isotope/isotope';
// Add the sorting options to the initial presentation.
$additional_settings = [
'getSortData' => [],
];
foreach ($variables['items'] as $item) {
$item['data'] = !empty($item['data']) ? $item['data'] : [];
foreach ($item['data'] as $key => $value) {
$additional_settings['getSortData'][$key] = '.' . $key;
}
}
// Retrieve the desired configuration (plus sorting options).
$presentation_json = $presentation
->toJSON($additional_settings);
// Make sure the instance name is unique per page load.
$instance_name = 0;
$global_instances =& drupal_static(__FUNCTION__);
$global_instances = isset($global_instances) ? $global_instances : [];
if (!empty($variables['instance']) && !in_array($variables['instance'], $global_instances)) {
$instance_name = $variables['instance'];
}
else {
for ($i = 0; $i >= 0; $i++) {
if (!in_array($i, $global_instances)) {
$instance_name = $i;
// Break the infinite loop when successful.
break;
}
}
}
$global_instances[] = $instance_name;
$instance = 'isotope-instance-' . $instance_name;
$items = [
[
'#markup' => '',
'#wrapper_attributes' => [
'class' => [
'js-isotope-grid-sizer',
],
],
],
[
'#markup' => '',
'#wrapper_attributes' => [
'class' => [
'js-isotope-gutter-sizer',
],
],
],
];
foreach ($variables['items'] as $item) {
$sorts = '';
$item['data'] = !empty($item['data']) ? $item['data'] : [];
foreach ($item['data'] as $key => $value) {
if (!is_array($value)) {
$value = [
$value,
];
}
foreach ($value as $sort) {
$sorts .= '<div class="js-sort-data ' . $key . '">' . IsotopeController::sanitize($sort) . '</div>';
}
// Sanitize value for the class name below.
$item['data'][$key] = IsotopeController::sanitize($value);
}
$classes = array_values($item['data']);
$classes[] = 'js-isotope-element';
$items[] = [
'#markup' => $item['value'] . $sorts,
'#wrapper_attributes' => [
'class' => $classes,
],
];
}
$variables['grid'] = [
'#theme' => 'item_list',
'#items' => $items,
'#type' => 'ul',
'#attributes' => [
'class' => 'js-isotope-grid',
'id' => $instance,
'data-isotope' => $presentation_json,
],
'#attached' => [
'library' => $libraries,
],
];
}