You are here

public function BackgroundMedia::buildStyleFormElements in Bootstrap Styles 1.0.x

Overrides StylePluginBase::buildStyleFormElements

File

src/Plugin/BootstrapStyles/Style/BackgroundMedia.php, line 226

Class

BackgroundMedia
Class BackgroundMedia.

Namespace

Drupal\bootstrap_styles\Plugin\BootstrapStyles\Style

Code

public function buildStyleFormElements(array &$form, FormStateInterface $form_state, $storage) {
  $icon_path = drupal_get_path('module', 'bootstrap_styles') . '/images/';
  $form['background_type']['#options']['image'] = $this
    ->getSvgIconMarkup($icon_path . 'plugins/background/background-image.svg');
  $form['background_type']['#options']['video'] = $this
    ->getSvgIconMarkup($icon_path . 'plugins/background/background-video.svg');
  if (!$form['background_type']['#default_value']) {
    $form['background_type']['#default_value'] = $storage['background']['background_type'] ?? 'image';
  }

  // Background media.
  $config = $this
    ->config();

  // Check if the bundle exist.
  if ($config
    ->get('background_image.bundle') && $this->entityTypeManager
    ->getStorage('media_type')
    ->load($config
    ->get('background_image.bundle'))) {
    $form['background_image'] = [
      '#type' => 'media_library',
      '#title' => $this
        ->t('Background image'),
      '#description' => $this
        ->t('Background image'),
      '#allowed_bundles' => [
        $config
          ->get('background_image.bundle'),
      ],
      '#default_value' => $storage['background_media']['image']['media_id'] ?? NULL,
      '#states' => [
        'visible' => [
          ':input.bs_background--type' => [
            'value' => 'image',
          ],
        ],
      ],
    ];
  }

  // Check if the bundle exist.
  if ($config
    ->get('background_local_video.bundle') && $this->entityTypeManager
    ->getStorage('media_type')
    ->load($config
    ->get('background_local_video.bundle'))) {
    $form['background_video'] = [
      '#type' => 'media_library',
      '#title' => $this
        ->t('Background video'),
      '#description' => $this
        ->t('Background video'),
      '#allowed_bundles' => [
        $config
          ->get('background_local_video.bundle'),
      ],
      '#default_value' => $storage['background_media']['video']['media_id'] ?? NULL,
      '#states' => [
        'visible' => [
          ':input.bs_background--type' => [
            'value' => 'video',
          ],
        ],
      ],
    ];
  }
  $form['background_options'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'bs_background--options bs_row',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input.bs_background--type' => [
          'value' => 'image',
        ],
      ],
    ],
  ];
  $form['background_options']['background_position'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Position'),
    '#options' => [
      'left top' => $this
        ->t('Left Top'),
      'center top' => $this
        ->t('Center Top'),
      'right top' => $this
        ->t('Right Top'),
      'left center' => $this
        ->t('Left Center'),
      'center' => $this
        ->t('Center'),
      'right center' => $this
        ->t('Right Center'),
      'left bottom' => $this
        ->t('Left Bottom'),
      'center bottom' => $this
        ->t('Center Bottom'),
      'right bottom' => $this
        ->t('Right Bottom'),
    ],
    '#default_value' => $storage['background_media']['background_options']['background_position'] ?? 'center',
    '#attributes' => [
      'class' => [
        'bs_background--position bs_col bs_col--50',
      ],
    ],
  ];
  $form['background_options']['background_repeat'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Repeat'),
    '#options' => [
      'no-repeat' => $this
        ->getSvgIconMarkup($icon_path . 'plugins/background-repeat/background-no-repeat.svg'),
      'repeat' => $this
        ->getSvgIconMarkup($icon_path . 'plugins/background-repeat/background-repeat.svg'),
      'repeat-x' => $this
        ->getSvgIconMarkup($icon_path . 'plugins/background-repeat/background-repeat-xy.svg'),
      'repeat-y' => $this
        ->getSvgIconMarkup($icon_path . 'plugins/background-repeat/background-repeat-xy.svg'),
    ],
    '#default_value' => $storage['background_media']['background_options']['background_repeat'] ?? 'no-repeat',
    '#attributes' => [
      'class' => [
        'bs_background--repeat bs_col bs_col--50',
      ],
    ],
  ];
  $form['background_options']['background_attachment'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Attachment'),
    '#options' => [
      'not_fixed' => $this
        ->t('Not Fixed'),
      'fixed' => $this
        ->t('Fixed'),
    ],
    '#default_value' => $storage['background_media']['background_options']['background_attachment'] ?? 'not_fixed',
    '#attributes' => [
      'class' => [
        'bs_background--attachment bs_col bs_col--100',
      ],
    ],
    '#prefix' => '<hr class="bs_divider"/>',
    '#suffix' => '<hr class="bs_divider"/>',
  ];
  $form['background_options']['background_size'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Size'),
    '#options' => [
      'cover' => $this
        ->t('Cover'),
      'contain' => $this
        ->t('Contain'),
      'auto' => $this
        ->t('Auto'),
    ],
    '#default_value' => $storage['background_media']['background_options']['background_size'] ?? 'cover',
    '#attributes' => [
      'class' => [
        'bs_background--size bs_col bs_col--100',
      ],
    ],
    '#suffix' => '<hr class="bs_divider"/>',
  ];
  return $form;
}