You are here

class IsotopeController in Isotope (with Masonry and Packery) 8

Controller routines for admin block routes.

Hierarchy

Expanded class hierarchy of IsotopeController

2 files declare their use of IsotopeController
isotope.theme.inc in ./isotope.theme.inc
Preprocessors of isotope module.
Presentation.php in src/Entity/Presentation.php
Contains \Drupal\isotope\Entity\Presentation.

File

src/Controller/IsotopeController.php, line 18
Contains \Drupal\isotope\Controller\IsotopeController.

Namespace

Drupal\isotope\Controller
View source
class IsotopeController extends ControllerBase {

  /**
   * The transliteration service.
   *
   * @var \Drupal\Component\Transliteration\TransliterationInterface
   */
  protected $transliteration;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  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;
  }

  /**
   * Given a layout name return the name of a library to load.
   *
   * @param string|array $properties
   *   Name of layout mode or other plugin.
   *
   * @return array
   *   Array of library names to load, keyed by property name.
   */
  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));
  }

  /**
   * Convert a snake_case string to a camelCase string.
   *
   * @param string $raw
   *   String in snake_case.
   *
   * @return string
   *   String in camelCase.
   */
  public static function snakeToCamel($raw) {
    return lcfirst(str_replace(' ', '', ucwords(str_replace([
      '-',
      '_',
    ], ' ', $raw))));
  }

  /**
   * Strip all special chars to make value suitable for css class name.
   *
   * @param string|array $raw
   *   The raw value to be sanitized. Arrays will be imploded by space.
   *
   * @return string
   *   The sanitized value.
   */
  public function sanitize($raw) {
    $safe = $raw;

    // Recursively sanitize arrays.
    if (is_array($raw)) {
      $safe = [];
      foreach ($raw as $i) {
        $safe[] = $this
          ->sanitize($i);
      }
      return implode(' ', $safe);
    }

    // Transliterate non-latin characters.
    // @TODO: Using services means stop using static.
    $language_code = $this->languageManager
      ->getDefaultLanguage()
      ->getId();
    $safe = $this->transliteration
      ->transliterate($safe, $language_code);

    // Basic class-name rules.
    $safe = strtolower($safe);
    $safe = preg_replace('/[^a-z0-9]/s', '-', $safe);
    $safe = preg_replace('/-{2,}/', '-', $safe);
    return $safe;
  }

  /**
   * Returns a demo page
   *
   * @TODO: This should be in an example sub-module.
   *
   * @return array
   *   A #type 'page' render array containing the block region demo.
   */
  public function demo() {
    $render[] = [
      '#markup' => $this
        ->t('<p>You can explore this example for yourself at <strong>___</strong>.</p>'),
    ];

    // The label can be anything, including images, etc. Filtering happens on the
    // key.
    $colors = [
      'blue' => 'Blue',
      'red' => '<span class="thisIsCustomisedLabel">Red</span>',
      'yellow' => 'Yellow',
    ];
    $render[] = [
      '#theme' => 'isotope_filter',
      '#items' => $colors,
      // Optional filter_name: unique to distinguish it from the other filters on
      // the page.
      '#filter_name' => 'color',
      // Optional filter_title: displayed as a list title.
      '#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',
      // Transliterate non-latin characters.
      '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,
    ];

    // Create items of every size shape and color.
    $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
IsotopeController::$languageManager protected property The language manager. Overrides ControllerBase::$languageManager
IsotopeController::$transliteration protected property The transliteration service.
IsotopeController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
IsotopeController::demo public function Returns a demo page
IsotopeController::libraryMapping public static function Given a layout name return the name of a library to load.
IsotopeController::sanitize public function Strip all special chars to make value suitable for css class name.
IsotopeController::snakeToCamel public static function Convert a snake_case string to a camelCase string.
IsotopeController::__construct public function
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.