You are here

class GridStackAdmin in GridStack 8

Same name and namespace in other branches
  1. 8.2 src/Form/GridStackAdmin.php \Drupal\gridstack\Form\GridStackAdmin

Provides resusable admin functions or form elements.

Hierarchy

Expanded class hierarchy of GridStackAdmin

1 file declares its use of GridStackAdmin
GridStackAdminUnitTest.php in tests/src/Unit/Form/GridStackAdminUnitTest.php
1 string reference to 'GridStackAdmin'
gridstack.services.yml in ./gridstack.services.yml
gridstack.services.yml
1 service uses GridStackAdmin
gridstack.admin in ./gridstack.services.yml
Drupal\gridstack\Form\GridStackAdmin

File

src/Form/GridStackAdmin.php, line 15

Namespace

Drupal\gridstack\Form
View source
class GridStackAdmin implements GridStackAdminInterface {
  use StringTranslationTrait;

  /**
   * The blazy admin service.
   *
   * @var \Drupal\blazy\Form\BlazyAdminInterface
   */
  protected $blazyAdmin;

  /**
   * The gridstack manager service.
   *
   * @var \Drupal\gridstack\GridStackManagerInterface
   */
  protected $manager;

  /**
   * Constructs a GridStackAdmin object.
   *
   * @param \Drupal\blazy\Form\BlazyAdminInterface $blazy_admin
   *   The blazy admin service.
   * @param \Drupal\gridstack\GridStackManagerInterface $manager
   *   The gridstack manager service.
   */
  public function __construct(BlazyAdminInterface $blazy_admin, GridStackManagerInterface $manager) {
    $this->blazyAdmin = $blazy_admin;
    $this->manager = $manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('blazy.admin.extended'), $container
      ->get('gridstack.manager'));
  }

  /**
   * Returns the blazy admin formatter.
   */
  public function blazyAdmin() {
    return $this->blazyAdmin;
  }

  /**
   * Returns the slick manager.
   */
  public function manager() {
    return $this->manager;
  }

  /**
   * Returns all settings form elements.
   */
  public function buildSettingsForm(array &$form, $definition = []) {
    $definition['namespace'] = 'gridstack';
    $definition['skins'] = $this
      ->getSkinOptions();
    $definition['style'] = FALSE;
    $definition['grid_form'] = FALSE;
    $optionsets = [];
    $entities = $this->manager
      ->entityLoadMultiple('gridstack');
    foreach ($entities as $key => $entity) {

      // Exludes Boostrap/ Foundation grids which only work for DS, Panels.
      if ($entity
        ->getOption('use_framework')) {
        continue;
      }
      $optionsets[$key] = Html::escape($entity
        ->label());
    }
    $definition['optionsets'] = $optionsets;
    foreach ([
      'background',
      'caches',
      'fieldable_form',
      'id',
      'vanilla',
    ] as $key) {
      $definition[$key] = isset($definition[$key]) ? $definition[$key] : TRUE;
    }
    $definition['layouts'] = isset($definition['layouts']) ? array_merge($this
      ->getLayoutOptions(), $definition['layouts']) : $this
      ->getLayoutOptions();
    $this
      ->openingForm($form, $definition);
    $this
      ->mainForm($form, $definition);
    $this
      ->closingForm($form, $definition);
  }

  /**
   * Returns the opening form elements.
   */
  public function openingForm(array &$form, $definition = []) {
    $path = drupal_get_path('module', 'gridstack');
    $readme = Url::fromUri('base:' . $path . '/README.txt')
      ->toString();
    if (!isset($form['optionset'])) {
      $this->blazyAdmin
        ->openingForm($form, $definition);
      if ($this
        ->manager()
        ->getModuleHandler()
        ->moduleExists('gridstack_ui')) {
        $route_name = 'entity.gridstack.collection';
        $form['optionset']['#description'] = $this
          ->t('Manage optionsets at <a href=":url" target="_blank">the optionset admin page</a>.', [
          ':url' => Url::fromRoute($route_name)
            ->toString(),
        ]);
      }
    }
    if (isset($form['skin'])) {
      $form['skin']['#description'] = $this
        ->t('Skins allow various layouts with just CSS. Some options below depend on a skin. Leave empty to DIY. Or use hook_gridstack_skins_info() and implement \\Drupal\\gridstack\\GridStackSkinInterface to register ones.', [
        ':url' => $readme,
      ]);
    }
    if (isset($form['background'])) {
      $form['background']['#description'] = $this
        ->t('If trouble with image sizes not filling the given box, check this to turn the image into CSS background instead. To assign different image style per grid/box, edit the working optionset.');
    }
  }

  /**
   * Returns the main form elements.
   */
  public function mainForm(array &$form, $definition = []) {
    if (!empty($definition['image_style_form'])) {
      $this->blazyAdmin
        ->imageStyleForm($form, $definition);
    }
    if (!empty($definition['media_switch_form'])) {
      $this->blazyAdmin
        ->mediaSwitchForm($form, $definition);
    }
    if (!empty($definition['fieldable_form'])) {
      $this->blazyAdmin
        ->fieldableForm($form, $definition);
    }
    if (!empty($definition['breakpoints'])) {
      $this->blazyAdmin
        ->breakpointsForm($form, $definition);
    }
  }

  /**
   * Returns the closing ending form elements.
   */
  public function closingForm(array &$form, $definition = []) {
    if (!isset($form['cache'])) {
      $this->blazyAdmin
        ->closingForm($form, $definition);
    }
    $form['#attached']['library'][] = 'gridstack/admin';
  }

  /**
   * Returns available skins for select options.
   */
  public function getSkinOptions() {
    return $this->manager
      ->getSkinOptions();
  }

  /**
   * Returns default layout options for the core Image, or Views.
   */
  public function getLayoutOptions() {
    return [
      'bottom' => $this
        ->t('Caption bottom'),
      'center' => $this
        ->t('Caption center'),
      'top' => $this
        ->t('Caption top'),
    ];
  }

  /**
   * Return the field formatter settings summary.
   *
   * @deprecated: Removed for self::getSettingsSummary().
   */
  public function settingsSummary($plugin, $definition = []) {
    return $this->blazyAdmin
      ->settingsSummary($plugin, $definition);
  }

  /**
   * Return the field formatter settings summary.
   *
   * @todo: Remove second param $plugin for post-release for Blazy RC2+.
   */
  public function getSettingsSummary($definition = [], $plugin = NULL) {

    // @todo: Remove condition for Blazy RC2+.
    if (!method_exists($this->blazyAdmin, 'getSettingsSummary')) {
      return $this->blazyAdmin
        ->settingsSummary($plugin, $definition);
    }
    return $this->blazyAdmin
      ->getSettingsSummary($definition);
  }

  /**
   * Returns available fields for select options.
   */
  public function getFieldOptions($target_bundles = [], $allowed_field_types = [], $entity_type_id = 'media', $target_type = '') {
    return $this->blazyAdmin
      ->getFieldOptions($target_bundles, $allowed_field_types, $entity_type_id, $target_type);
  }

  /**
   * Returns re-usable logic, styling and assets across fields and Views.
   */
  public function finalizeForm(array &$form, $definition = []) {
    $this->blazyAdmin
      ->finalizeForm($form, $definition);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GridStackAdmin::$blazyAdmin protected property The blazy admin service.
GridStackAdmin::$manager protected property The gridstack manager service.
GridStackAdmin::blazyAdmin public function Returns the blazy admin formatter.
GridStackAdmin::buildSettingsForm public function Returns all settings form elements.
GridStackAdmin::closingForm public function Returns the closing ending form elements.
GridStackAdmin::create public static function
GridStackAdmin::finalizeForm public function Returns re-usable logic, styling and assets across fields and Views.
GridStackAdmin::getFieldOptions public function Returns available fields for select options.
GridStackAdmin::getLayoutOptions public function Returns default layout options for the core Image, or Views.
GridStackAdmin::getSettingsSummary public function Return the field formatter settings summary.
GridStackAdmin::getSkinOptions public function Returns available skins for select options.
GridStackAdmin::mainForm public function Returns the main form elements.
GridStackAdmin::manager public function Returns the slick manager.
GridStackAdmin::openingForm public function Returns the opening form elements.
GridStackAdmin::settingsSummary public function Return the field formatter settings summary.
GridStackAdmin::__construct public function Constructs a GridStackAdmin object.
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.