View source
<?php
namespace Drupal\isotope\Controller;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class IsotopeController extends ControllerBase {
protected $transliteration;
protected $languageManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('transliteration'), $container
->get('language_manager'));
}
public function __construct(TransliterationInterface $transliteration, LanguageManagerInterface $languageManager) {
$this->transliteration = $transliteration;
$this->languageManager = $languageManager;
}
public static function libraryMapping($properties) {
$properties = (array) $properties;
$mappings = [
'cellsByColumn' => 'isotope/isotope-cells-by-column',
'cellsByRow' => 'isotope/isotope-cells-by-row',
'fitColumns' => 'isotope/isotope-fit-columns',
'fitRows' => 'isotope/isotope-layout',
'horiz' => 'isotope/isotope-horizontal',
'imagesloaded' => 'isotope/imagesloaded',
'masonry' => 'isotope/isotope-layout',
'masonryHorizontal' => 'isotope/isotope-masonry-horizontal',
'packery' => 'isotope/packery',
'vertical' => 'isotope/isotope-layout',
];
return array_intersect_key($mappings, array_flip($properties));
}
public static function snakeToCamel($raw) {
return lcfirst(str_replace(' ', '', ucwords(str_replace([
'-',
'_',
], ' ', $raw))));
}
public function sanitize($raw) {
$safe = $raw;
if (is_array($raw)) {
$safe = [];
foreach ($raw as $i) {
$safe[] = $this
->sanitize($i);
}
return implode(' ', $safe);
}
$language_code = $this->languageManager
->getDefaultLanguage()
->getId();
$safe = $this->transliteration
->transliterate($safe, $language_code);
$safe = strtolower($safe);
$safe = preg_replace('/[^a-z0-9]/s', '-', $safe);
$safe = preg_replace('/-{2,}/', '-', $safe);
return $safe;
}
public function demo() {
$render[] = [
'#markup' => $this
->t('<p>You can explore this example for yourself at <strong>___</strong>.</p>'),
];
$colors = [
'blue' => 'Blue',
'red' => '<span class="thisIsCustomisedLabel">Red</span>',
'yellow' => 'Yellow',
];
$render[] = [
'#theme' => 'isotope_filter',
'#items' => $colors,
'#filter_name' => 'color',
'#filter_title' => $this
->t('Colour'),
];
$sizes = [
'small' => 'Small',
'wide' => 'Wide',
'big' => 'Big',
'tall' => 'Tall',
];
$render[] = [
'#theme' => 'isotope_filter',
'#items' => $sizes,
'#filter_name' => 'size',
'#filter_title' => $this
->t('Size'),
];
$shapes = [
'round' => 'Round',
'square (naïve jalapeño pâté)' => 'Square',
];
$render[] = [
'#theme' => 'isotope_filter',
'#items' => $shapes,
'#filter_name' => 'shape',
'#filter_title' => $this
->t('Shapes'),
'#instance' => 1,
];
$sorts = [
'Size' => 'size',
'shape',
'color',
[
'color',
'size',
],
'Color then Shape' => [
'color',
'shape',
],
];
$render[] = [
'#theme' => 'isotope_sorter',
'#sorts' => $sorts,
'#default_label' => $this
->t('Original'),
'#instance' => 1,
];
$items = [];
foreach ($sizes as $size => $label1) {
foreach ($shapes as $shape => $label2) {
foreach ($colors as $color => $label3) {
$items[] = [
'value' => '<p>Item</p>',
'data' => [
'size' => $size,
'shape' => $shape,
'color' => $color,
],
];
}
}
}
$render['grid'] = [
'#theme' => 'isotope_grid',
'#items' => $items,
'#instance' => 1,
'#attached' => [
'library' => [
'isotope/isotope-example',
],
],
];
return $render;
}
}