gridstack-8.x-2.5/src/Plugin/Layout/GridStackLayoutTool.php

src/Plugin/Layout/GridStackLayoutTool.php
<?php

namespace Drupal\gridstack\Plugin\Layout;

use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Provides common utilities for Layout plugins.
 */
class GridStackLayoutTool {

  /**
   * The classes preset.
   *
   * @var array
   */
  private static $classes;

  /**
   * Returns classes that can be used for select options.
   *
   * @param bool $flatten
   *   Whether flattened or grouped.
   *
   * @return array
   *   An associative array of grouped, or flattened classes.
   */
  public static function classes($flatten = FALSE) {
    if (!isset(static::$classes[$flatten])) {
      static::$classes[$flatten] = [];
      if ($framework = \gridstack()->configLoad('framework', 'gridstack.settings')) {
        static::$classes[$flatten] = self::presets($flatten, $framework);
      }
    }
    return static::$classes[$flatten];
  }

  /**
   * Returns classes that can be used for select options.
   *
   * @param bool $flatten
   *   Whether flattened or grouped.
   * @param string $framework
   *   The supported CSS frameworks: bootstrap3, bootstrap, foundations.
   *
   * @return array
   *   An associative array of grouped, or flattened classes.
   */
  protected static function presets($flatten = FALSE, $framework = 'foundation') {
    $classes = strpos($framework, 'foundation') !== FALSE ? GridStackFoundation::classes($flatten) : GridStackBootstrap::classes($flatten);
    $fw_classes = \gridstack()->configLoad('fw_classes', 'gridstack.settings');
    if (!empty($fw_classes)) {
      $fw_classes = array_map('trim', explode("\n", $fw_classes));
      foreach ($fw_classes as $fw_class) {
        if (strpos($fw_class, '|') !== FALSE) {
          list($group, $group_class) = array_pad(array_map('trim', explode("|", $fw_class, 2)), 2, NULL);
          $group_classes = array_map('trim', explode(" ", $group_class));
          $new_group = [];
          foreach ($group_classes as $group_class) {
            $new_group[] = $group_class;
          }

          $new_group = isset($classes[$group]) ? NestedArray::mergeDeep($classes[$group], $new_group) : $new_group;
          $classes[$group] = array_unique($new_group);
        }
      }
    }

    $grouped = $ungrouped = [];
    foreach ($classes as $group => $data) {
      $grouped[$group] = $data;
      $items = [];
      foreach ($data as $datum) {
        $items[] = $datum;
      }

      $ungrouped = NestedArray::mergeDeep($ungrouped, $items);
    }

    // Do not use array_unique() here, was processed per group instead.
    $output = $flatten ? $ungrouped : $grouped;
    \gridstack()->getModuleHandler()->alter('gridstack_classes_preset', $output, $grouped, $ungrouped, $flatten, $framework);

    ksort($output);
    return $output;
  }

  /**
   * Returns merged $wrapper_classes and $selected_classes.
   */
  public static function mergedClasses($wrapper_class = '', $selected_classes = []) {
    $classes = [];
    if ($classes = self::classes(TRUE)) {
      $classes = array_combine($classes, $classes);
    }

    $wrapper_classes = $wrapper_class ? array_map('trim', explode(" ", $wrapper_class)) : [];
    $wrapper_classes = $wrapper_class ? array_combine($wrapper_classes, $wrapper_classes) : [];

    $selected_classes = array_filter($selected_classes);
    if ($selected_classes) {
      $selected_classes = array_values($selected_classes);
      $selected_classes = array_combine($selected_classes, $selected_classes);
    }

    // If $selected_classes are left empty, remove it from $wrapper_classes.
    // Ensures to not remove custom defined classes.
    if ($wrapper_classes) {
      foreach ($wrapper_classes as $key => $value) {
        if (isset($classes[$key]) && !isset($selected_classes[$key])) {
          unset($wrapper_classes[$key]);
        }
      }
    }

    $wrapper_classes = $selected_classes ? array_merge($wrapper_classes, $selected_classes) : $wrapper_classes;
    $wrapper_classes = array_unique(array_values($wrapper_classes));
    return $wrapper_classes ? implode(" ", $wrapper_classes) : '';
  }

  /**
   * Returns classes for select options.
   *
   * @param array $options
   *   The $options of a current group.
   *
   * @return array
   *   An array of translatable $options.
   */
  public static function options(array $options = []) {
    $formatted_options = [];
    foreach ($options as $option) {
      $title = str_replace(['bg-', 'text-', 'hidden-', 'visible-'], '', $option);
      $formatted_options[$option] = new TranslatableMarkup('@title', ['@title' => Unicode::ucfirst($title)]);
    }

    \gridstack()->getModuleHandler()->alter('gridstack_classes_preset_options', $formatted_options);

    ksort($formatted_options);
    return $formatted_options;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc