You are here

public function PresentationFormBase::buildForm in Isotope (with Masonry and Packery) 8

Overrides Drupal\Core\Entity\EntityFormController::form().

Builds the entity add/edit form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: An associative array containing the current state of the form.

Return value

array An associative array containing the Presentation add/edit form.

Overrides EntityForm::buildForm

File

src/Form/PresentationFormBase.php, line 76
Contains Drupal\isotope\Form\PresentationFormBase.

Class

PresentationFormBase
Class PresentationFormBase.

Namespace

Drupal\isotope\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get anything we need from the base class.
  $form = parent::buildForm($form, $form_state);

  // Drupal provides the entity to us as a class variable. If this is an
  // existing entity, it will be populated with existing values as class
  // variables. If this is a new entity, it will be a new object with the
  // class of our entity. Drupal knows which class to call from the
  // annotation on our Presentation class.
  $presentation = $this->entity;

  // Build the form.
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $presentation
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Machine name'),
    '#default_value' => $presentation
      ->id(),
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
      'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
      'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".',
    ],
    '#disabled' => !$presentation
      ->isNew(),
  ];

  // Layout Modes.
  $layouts = [
    'cellsByColumn' => t('cellsByColumn'),
    'cellsByRow' => t('cellsByRow'),
    'fitColumns' => t('fitColumns'),
    'fitRows' => t('fitRows'),
    'horiz' => t('horiz'),
    'masonry' => t('masonry'),
    'masonryHorizontal' => t('masonryHorizontal'),
    'packery' => t('packery'),
    'vertical' => t('vertical'),
  ];
  $form['layout_mode'] = [
    '#type' => 'radios',
    '#options' => $layouts,
    '#title' => t('Isotope Layout Mode'),
    // '#description' => t('If desired options are disabled, you may need to install additional JS libraries. Refer to installation instructions.'),
    '#default_value' => $presentation->layout_mode ? $presentation->layout_mode : 'masonry',
    '#required' => FALSE,
  ];

  // Non-layout plugins.
  $plugins = [
    'imagesloaded' => t('imagesloaded'),
  ];
  $form['plugins'] = [
    '#type' => 'checkboxes',
    '#options' => $plugins,
    '#title' => t('Additional Plugins'),
    // '#description' => t('If desired options are disabled, you may need to install additional JS libraries.'),
    '#default_value' => $presentation->plugins ? $presentation->plugins : [
      'imagesloaded',
    ],
    '#required' => FALSE,
    '#tree' => TRUE,
  ];
  $form['transition_duration'] = [
    '#type' => 'textfield',
    '#title' => t('Transition Duration'),
    '#description' => t('In a format suitable for CSS transition-duration (e.g. "0.2s"). To disable all transitions, set to "0".'),
    '#default_value' => $presentation->transition_duration,
    '#required' => FALSE,
  ];
  $form['url_filters'] = [
    '#type' => 'select',
    '#options' => [
      0 => 'False',
      1 => 'True',
    ],
    '#title' => t('Use URL for Filters.'),
    '#description' => t('Filters are represented in URL for benefit of browser history, bookmarking, etc.'),
    '#default_value' => $presentation->url_filters,
    '#required' => FALSE,
  ];
  $form['is_fit_width'] = [
    '#type' => 'select',
    '#options' => [
      0 => 'False',
      1 => 'True',
    ],
    '#title' => t('isFitWidth'),
    '#description' => t("Sets the width of the container to fit the available number of columns, based the size of container's parent element. When enabled, you can center the container with CSS."),
    '#default_value' => $presentation->is_fit_width,
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="layoutMode"]' => [
          "value" => 'masonry',
        ],
      ],
    ],
  ];
  $form['is_horizontal'] = [
    '#type' => 'select',
    '#options' => [
      0 => 'False',
      1 => 'True',
    ],
    '#title' => t('isHorizontal'),
    '#description' => t('Arranges items horizontally instead of vertically.'),
    '#default_value' => $presentation->is_horizontal,
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="layoutMode"]' => [
          "value" => 'packery',
        ],
      ],
    ],
  ];
  $form['stamp'] = [
    '#type' => 'textfield',
    '#title' => t('Stamp Selector'),
    '#description' => t('Specifies elements that are stamped within the layout. These are special layout elements which will not be laid out. Rather, Isotope will layout item elements below stamped elements.'),
    '#default_value' => $presentation->stamp,
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        [
          [
            ':input[name="layoutMode"]' => [
              'value' => 'masonry',
            ],
          ],
          [
            ':input[name="layoutMode"]' => [
              'value' => 'packery',
            ],
          ],
          [
            ':input[name="layoutMode"]' => [
              'value' => 'masonryHorizontal',
            ],
          ],
        ],
      ],
    ],
  ];
  $form['horizontal_alignment'] = [
    '#type' => 'textfield',
    '#title' => t('Horizontal Alignment (decimal number 0 to 1)'),
    '#description' => t('Aligns items horizontally. 0 will align the origin edge. 1 will align the opposite edge. 0.5 will align center.'),
    '#default_value' => $presentation->horizontal_alignment,
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="layoutMode"]' => [
          "value" => 'vertical',
        ],
      ],
    ],
  ];
  $form['vertical_alignment'] = [
    '#type' => 'textfield',
    '#title' => t('Vertical Alignment (decimal number 0 to 1)'),
    '#description' => t('Aligns items vertically. 0 will align the origin edge. 1 will align the opposite edge. 0.5 will align center.'),
    '#default_value' => $presentation->vertical_alignment,
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="layoutMode"]' => [
          "value" => 'horizontal',
        ],
      ],
    ],
  ];
  $form['is_origin_left'] = [
    '#type' => 'select',
    '#options' => [
      1 => 'Left to Right',
      0 => 'Right to Left',
    ],
    '#title' => t('Direction'),
    '#description' => t('Layout direction (implements "isOriginLeft").'),
    '#default_value' => $presentation->is_origin_left,
    '#required' => FALSE,
  ];

  // Return the form.
  return $form;
}