You are here

public function WebformSubmissionLimitBlock::blockForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/Block/WebformSubmissionLimitBlock.php \Drupal\webform\Plugin\Block\WebformSubmissionLimitBlock::blockForm()

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/WebformSubmissionLimitBlock.php, line 132

Class

WebformSubmissionLimitBlock
Provides a 'Webform submission limit' block.

Namespace

Drupal\webform\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {

  // General.
  $form['general'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('General settings'),
    '#open' => TRUE,
  ];
  $form['general']['type'] = [
    '#title' => $this
      ->t('Display limit and total submissions for the'),
    '#type' => 'select',
    '#options' => [
      'webform' => $this
        ->t('Current webform'),
      'user' => $this
        ->t('Current user'),
    ],
    '#ajax' => static::getTokenAjaxSettings(),
    '#default_value' => $this->configuration['type'],
    '#parents' => [
      'settings',
      'type',
    ],
  ];
  $form['general']['source_entity'] = [
    '#title' => $this
      ->t('Restrict limit and total submissions to current or specified source entity'),
    '#type' => 'checkbox',
    '#return_value' => TRUE,
    '#ajax' => static::getTokenAjaxSettings(),
    '#default_value' => $this->configuration['source_entity'],
    '#parents' => [
      'settings',
      'source_entity',
    ],
  ];
  $form['general']['content'] = [
    '#type' => 'webform_html_editor',
    '#title' => $this
      ->t('Content'),
    '#description' => $this
      ->t('The entered text appears before the progress bar.'),
    '#default_value' => $this->configuration['content'],
    '#parents' => [
      'settings',
      'content',
    ],
  ];

  // Tokens.
  $form['tokens'] = static::buildTokens($this->configuration['type'], $this->configuration['source_entity']);

  // Progress.
  $form['progress'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Progress bar'),
    '#open' => TRUE,
  ];
  $form['progress']['progress_bar'] = [
    '#title' => $this
      ->t('Show progress bar'),
    '#type' => 'checkbox',
    '#return_value' => TRUE,
    '#default_value' => $this->configuration['progress_bar'],
    '#parents' => [
      'settings',
      'progress_bar',
    ],
  ];
  $form['progress']['progress_bar_label'] = [
    '#title' => $this
      ->t('Progress bar label'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('The entered text appears above the progress bar.'),
    '#default_value' => $this->configuration['progress_bar_label'],
    '#parents' => [
      'settings',
      'progress_bar_label',
    ],
    '#states' => [
      'visible' => [
        ':input[name="settings[progress_bar]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['progress']['progress_bar_message'] = [
    '#title' => $this
      ->t('Progress bar message'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('The entered text appears below the progress bar.'),
    '#default_value' => $this->configuration['progress_bar_message'],
    '#parents' => [
      'settings',
      'progress_bar_message',
    ],
    '#states' => [
      'visible' => [
        ':input[name="settings[progress_bar]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Advanced.
  $form['advanced'] = [
    '#title' => $this
      ->t('Advanced settings'),
    '#type' => 'details',
    '#description' => $this
      ->t("Webform and source entity are automatically detected based on the current page request. You can use the below settings to hardcode the submission limit block's webform and source entity."),
    '#open' => $this->configuration['webform_id'] || $this->configuration['entity_type'],
  ];
  $form['advanced']['webform_id'] = [
    '#title' => $this
      ->t('Webform'),
    '#type' => 'entity_autocomplete',
    '#target_type' => 'webform',
    '#default_value' => $this->configuration['webform_id'] ? $this->entityTypeManager
      ->getStorage('webform')
      ->load($this->configuration['webform_id']) : NULL,
    '#parents' => [
      'settings',
      'webform_id',
    ],
  ];
  $entity_type_options = [];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type) {
    $entity_type_options[$entity_type_id] = $entity_type
      ->getLabel();
  }
  $form['advanced']['entity_type'] = [
    '#type' => 'select',
    '#title' => 'Source entity type',
    '#empty_option' => $this
      ->t('- None -'),
    '#options' => $entity_type_options,
    '#default_value' => $this->configuration['entity_type'],
    '#parents' => [
      'settings',
      'entity_type',
    ],
    '#states' => [
      'visible' => [
        ':input[name="settings[advanced][webform_id]"]' => [
          'filled' => TRUE,
        ],
        ':input[name="settings[source_entity]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['advanced']['entity_id'] = [
    '#type' => 'textfield',
    '#title' => 'Source entity id',
    '#default_value' => $this->configuration['entity_id'],
    '#parents' => [
      'settings',
      'entity_id',
    ],
    '#states' => [
      'visible' => [
        ':input[name="settings[source_entity]"]' => [
          'checked' => TRUE,
        ],
        ':input[name="settings[advanced][webform_id]"]' => [
          'filled' => TRUE,
        ],
        ':input[name="settings[advanced][entity_type]"]' => [
          'filled' => TRUE,
        ],
      ],
    ],
  ];
  $this->tokenManager
    ->elementValidate($form);
  return $form;
}