You are here

public function DibaCarousel::blockForm in Diba carousel slider 8

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/DibaCarousel.php, line 148

Class

DibaCarousel
Provides a Diba carousel Block.

Namespace

Drupal\diba_carousel\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $form = parent::blockForm($form, $form_state);
  $config = $this
    ->getConfiguration();
  $defaults = $this
    ->defaultConfiguration();

  // If this method receives a subform state instead of the full form state.
  // @See https://www.drupal.org/node/2798261
  if ($form_state instanceof SubformStateInterface) {
    $ajax_values = $form_state
      ->getCompleteFormState()
      ->getValues();
  }
  else {
    $ajax_values = $form_state
      ->getValues();
  }

  // Ensure strongly that entity type is not null.
  if (isset($ajax_values['settings']['diba_carousel_settings']['content_selection']['entity_selected'])) {
    $config['entity_selected'] = $ajax_values['settings']['diba_carousel_settings']['content_selection']['entity_selected'];
  }
  if (empty($config['entity_selected'])) {
    $config['entity_selected'] = $defaults['entity_selected'];
  }

  // If all is empty ensures some field is rendered in slides.
  if (empty($config['image']) && empty($config['title']) && empty($config['description'])) {
    $config['title'] = $defaults['title'];
  }

  // Settings structure.
  $form['diba_carousel_settings'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Diba carousel configuration'),
    '#description' => $this
      ->t('Configure the diba carousel slider.'),
    '#attributes' => [
      'id' => 'diba-carousel-wrapper',
    ],
  ];
  $form['diba_carousel_settings']['content_selection'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Content selection and ordering'),
    '#description' => $this
      ->t('Filter, limit and sort the contents shown on slides.'),
    '#open' => TRUE,
    '#attributes' => [
      'id' => 'content-selection-wrapper',
    ],
  ];
  $form['diba_carousel_settings']['slide_fields'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Slide fields'),
    '#description' => $this
      ->t('Assign the fields used as image, title, description and link on slides.'),
    '#open' => TRUE,
    '#attributes' => [
      'id' => 'slide-fields-wrapper',
    ],
  ];
  $form['diba_carousel_settings']['styling'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Carousel styling'),
    '#description' => $this
      ->t('Carousel style, indicators and controls.'),
    '#open' => TRUE,
    '#attributes' => [
      'id' => 'styling-wrapper',
    ],
  ];

  // Content selection.
  $form['diba_carousel_settings']['content_selection']['entity_selected'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity type'),
    '#default_value' => $config['entity_selected'],
    '#options' => $this
      ->getEntityTypes(),
    '#description' => $this
      ->t('Check the entity that you want to use in the carousel. Default: node.'),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxFormSettingsCallback',
      ],
      'wrapper' => 'diba-carousel-wrapper',
      'event' => 'change',
    ],
  ];
  $bundles = $this
    ->getEntityTypeBundles($config['entity_selected']);
  if (!empty($bundles)) {

    // Wrap field with a div form-type-radios class to correct inline display.
    // See: https://www.drupal.org/project/drupal/issues/2992299
    $form['diba_carousel_settings']['content_selection']['content_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Entity type bundles'),
      '#default_value' => !empty($config['content_types']) ? $config['content_types'] : [],
      '#options' => $bundles,
      '#description' => $this
        ->t('Check the node content types that you want to filter in the carousel. If you uncheck all options, all nodes will be displayed if "node" entity type is checked and none if "node" entity is unchecked.'),
      '#prefix' => '<div class="form-type-radios">',
      '#sufix' => '</div>',
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
      '#validated' => TRUE,
    ];
  }
  $options = [
    'status' => $this
      ->t('Published'),
    'promote' => $this
      ->t('Promoted to front page'),
    'sticky' => $this
      ->t('Sticky at top of lists'),
  ];
  if ('node' === $config['entity_selected']) {

    // Add custom publishing options (custom_pub module integration).
    if ($this->moduleHandler
      ->moduleExists('custom_pub')) {
      $publish_types = $this->entityTypeManager
        ->getStorage('custom_publishing_option')
        ->loadMultiple();
      foreach ($publish_types as $publish_type) {
        $options[$publish_type
          ->id()] = $publish_type
          ->label();
      }
    }
  }
  $form['diba_carousel_settings']['content_selection']['publishing_options'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Publishing options'),
    '#default_value' => !empty($config['publishing_options']) ? $config['publishing_options'] : [],
    '#options' => $options,
    '#description' => $this
      ->t('Publishing options to filter content in the carousel.'),
    '#validated' => TRUE,
  ];
  $form['diba_carousel_settings']['content_selection']['skip_content_without_image'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Skip content without image'),
    '#default_value' => $config['skip_content_without_image'],
    '#description' => $this
      ->t('Ensure that all carousel content has image.'),
  ];
  $form['diba_carousel_settings']['content_selection']['filter_by_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Filter by field'),
    '#options' => $this
      ->getFields($config['entity_selected'], FALSE),
    '#default_value' => $config['filter_by_field'],
    '#description' => $this
      ->t('Select the field you want to use as filter.'),
    '#empty_option' => $this
      ->t('- None -'),
  ];
  $form['diba_carousel_settings']['content_selection']['filter_by_field_operator'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Filter by field operator'),
    '#options' => [
      '=' => $this
        ->t('Equal'),
      '<>' => $this
        ->t('Not equal'),
      'CONTAINS' => $this
        ->t('Contains'),
      '>' => $this
        ->t('Greater than'),
      '>=' => $this
        ->t('Greater than or equal'),
      '<' => $this
        ->t('Less than'),
      '<=' => $this
        ->t('Less than or equal'),
    ],
    '#default_value' => $config['filter_by_field_operator'],
    '#description' => $this
      ->t('Select the comparasion operator to use in filter field.'),
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][content_selection][filter_by_field]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  $form['diba_carousel_settings']['content_selection']['filter_by_field_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Filter by field value'),
    '#default_value' => $config['filter_by_field_value'],
    '#description' => $this
      ->t('Select the value you want to use as field filter. If you filter by field that contains taxonomy terms or relational content you should us the tid or entity id as value.'),
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][content_selection][filter_by_field]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  $option_types = [
    'integer',
    'created',
    'changed',
    'datetime',
    'string',
  ];
  $form['diba_carousel_settings']['content_selection']['order_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Order by'),
    '#options' => $this
      ->getFieldsByType($option_types, $config['entity_selected']),
    '#default_value' => $config['order_field'],
    '#empty_option' => $this
      ->t('- None -'),
    '#validated' => TRUE,
  ];
  $form['diba_carousel_settings']['content_selection']['order_direction'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Order direction'),
    '#options' => [
      'ASC' => $this
        ->t('Ascending'),
      'DESC' => $this
        ->t('Descending'),
      'RANDOM' => $this
        ->t('Random'),
    ],
    '#default_value' => $config['order_direction'],
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][content_selection][order_field]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  $form['diba_carousel_settings']['content_selection']['limit'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Max number of elements'),
    '#default_value' => $config['limit'],
    '#description' => $this
      ->t('The maximum number of elements to show in the carousel.'),
  ];

  // Slide fields and styling.
  $form['diba_carousel_settings']['styling']['carousel_style'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Carousel layout style'),
    '#options' => [
      'default' => $this
        ->t('Bootstrap default'),
      'diba' => $this
        ->t('Diba left captations'),
    ],
    '#default_value' => $config['carousel_style'],
    '#description' => $this
      ->t('The carousel style controls de visualization and carousel templating.'),
    '#required' => TRUE,
  ];
  $form['diba_carousel_settings']['styling']['show_indicators'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show indicators'),
    '#default_value' => $config['show_indicators'],
    '#description' => $this
      ->t('Show carousel indicators as squares or bullets.'),
  ];
  $form['diba_carousel_settings']['styling']['show_controls'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show controls'),
    '#default_value' => $config['show_controls'],
    '#description' => $this
      ->t('Show previous/next controls.'),
  ];
  $form['diba_carousel_settings']['styling']['items_by_slide'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Items by slide'),
    '#default_value' => (int) $config['items_by_slide'],
    '#options' => [
      1 => 1,
      2 => 2,
      3 => 3,
      4 => 4,
      6 => 6,
      12 => 12,
    ],
    '#description' => $this
      ->t('The number of items by slide.'),
    '#required' => TRUE,
  ];
  $form['diba_carousel_settings']['styling']['data_interval'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Slides interval'),
    '#default_value' => (int) $config['data_interval'],
    '#min' => 0,
    '#description' => $this
      ->t('The amount of time (in ms) to delay between automatically cycling an item. If 0, carousel will not automatically cycle.'),
  ];
  $form['diba_carousel_settings']['slide_fields']['image'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Image field'),
    '#options' => $this
      ->getFieldsByType([
      'image',
    ], $config['entity_selected']),
    '#default_value' => $config['image'],
    '#empty_option' => $this
      ->t('- None -'),
    '#validated' => TRUE,
  ];
  $form['diba_carousel_settings']['slide_fields']['image_style'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Image style'),
    '#options' => $this
      ->getImageStyles(),
    '#default_value' => $config['image_style'],
    '#empty_option' => $this
      ->t('- None -'),
    '#description' => $this
      ->t('Use an image style for scale, resize or crop images.'),
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][slide_fields][image]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  $form['diba_carousel_settings']['slide_fields']['image_class'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Image class'),
    '#default_value' => $config['image_class'],
    '#description' => $this
      ->t('Image class attribute. By default: img-fluid'),
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][slide_fields][image]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  $form['diba_carousel_settings']['slide_fields']['title'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Title field'),
    '#options' => $this
      ->getFieldsByType([
      'string',
    ], $config['entity_selected']),
    '#default_value' => $config['title'],
    '#empty_option' => $this
      ->t('- None -'),
    '#validated' => TRUE,
  ];
  $url_options = [
    'canonical' => $this
      ->t('Link to entity content'),
  ];
  $url_options = array_merge($url_options, $this
    ->getFieldsByType([
    'link',
  ], $config['entity_selected']));
  $form['diba_carousel_settings']['slide_fields']['url'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Link field'),
    '#options' => $url_options,
    '#default_value' => $config['url'],
    '#empty_option' => $this
      ->t('- None -'),
    '#validated' => TRUE,
  ];
  $option_types = [
    'text_with_summary',
    'text_long',
    'string',
    'entity_reference',
  ];
  $form['diba_carousel_settings']['slide_fields']['description'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Description field'),
    '#options' => $this
      ->getFieldsByType($option_types, $config['entity_selected']),
    '#default_value' => $config['description'],
    '#empty_option' => $this
      ->t('- None -'),
    '#validated' => TRUE,
  ];
  $form['diba_carousel_settings']['slide_fields']['description_allow_html'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow html description'),
    '#default_value' => $config['description_allow_html'],
    '#description' => $this
      ->t('If you use html description and truncate, the carousel will attempt to close the open tags.'),
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][slide_fields][description]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  $form['diba_carousel_settings']['slide_fields']['description_see_more_link'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Add see more link'),
    '#default_value' => $config['description_see_more_link'],
    '#description' => $this
      ->t('Add "See more" link at the end of description linking canonical entity.'),
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][slide_fields][description]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  $form['diba_carousel_settings']['slide_fields']['description_truncate'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum number of characters in description field'),
    '#default_value' => $config['description_truncate'],
    '#description' => $this
      ->t('Truncates the description to a maximum number of characters. Truncation attempts to truncate on a word boundary. Use 0 for unlimited.'),
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][slide_fields][description]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  $form['diba_carousel_settings']['slide_fields']['more_link'] = [
    '#type' => 'url',
    '#title' => $this
      ->t('More link'),
    '#default_value' => $config['more_link'],
    '#description' => $this
      ->t('This will add a more link to the bottom of the carousel.'),
  ];
  $form['diba_carousel_settings']['slide_fields']['more_link_text'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('More link text'),
    '#default_value' => $config['more_link_text'],
    '#description' => $this
      ->t('More link text.'),
    '#states' => [
      'visible' => [
        ':input[name="settings[diba_carousel_settings][slide_fields][more_link]"]' => [
          'filled' => TRUE,
        ],
      ],
    ],
  ];
  return $form;
}