You are here

public function EmbedButtonForm::form in Embed 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/EmbedButtonForm.php, line 66

Class

EmbedButtonForm
Form controller for embed button forms.

Namespace

Drupal\embed\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\embed\EmbedButtonInterface $button */
  $button = $this->entity;
  $form_state
    ->setTemporaryValue('embed_button', $button);
  $form['label'] = [
    '#title' => $this
      ->t('Label'),
    '#type' => 'textfield',
    '#default_value' => $button
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this embed button. This text will be displayed when the user hovers over the CKEditor button. This name must be unique.'),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $button
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#disabled' => !$button
      ->isNew(),
    '#machine_name' => [
      'exists' => [
        EmbedButton::class,
        'load',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this embed button. It must only contain lowercase letters, numbers, and underscores.'),
  ];
  $form['type_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Embed type'),
    '#options' => $this->embedTypeManager
      ->getDefinitionOptions(),
    '#default_value' => $button
      ->getTypeId(),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::updateTypeSettings',
      'effect' => 'fade',
    ],
    '#disabled' => !$button
      ->isNew(),
  ];
  if (empty($form['type_id']['#options'])) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('No embed types found.'));
  }

  // Add the embed type plugin settings.
  $form['type_settings'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div id="embed-type-settings-wrapper">',
    '#suffix' => '</div>',
  ];
  try {
    if ($plugin = $button
      ->getTypePlugin()) {
      $form['type_settings'] = $plugin
        ->buildConfigurationForm($form['type_settings'], $form_state);
    }
  } catch (PluginNotFoundException $exception) {
    $this
      ->messenger()
      ->addError($exception
      ->getMessage());
    watchdog_exception('embed', $exception);
    $form['type_id']['#disabled'] = FALSE;
  }
  $config = $this
    ->config('embed.settings');
  $upload_location = $config
    ->get('file_scheme') . '://' . $config
    ->get('upload_directory') . '/';
  $form['icon_file'] = [
    '#type' => 'managed_file',
    '#title' => $this
      ->t('Button icon'),
    '#upload_location' => $upload_location,
    '#upload_validators' => [
      'file_validate_extensions' => [
        'gif png jpg jpeg svg',
      ],
      'file_validate_image_resolution' => [
        '32x32',
        '16x16',
      ],
    ],
  ];
  if (!$button
    ->isNew()) {
    $form['icon_reset'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Reset to default icon'),
      '#access' => $button
        ->getIconUrl() !== $button
        ->getTypePlugin()
        ->getDefaultIconUrl(),
    ];
    $form['icon_preview'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Current icon preview'),
    ];
    $form['icon_preview']['image'] = [
      '#theme' => 'image',
      '#uri' => $button
        ->getIconUrl(),
      '#alt' => $this
        ->t('Preview of @label button icon', [
        '@label' => $button
          ->label(),
      ]),
    ];

    // Show an even nicer preview with CKEditor being used.
    if ($this->moduleHandler
      ->moduleExists('ckeditor')) {
      $form['icon_preview']['image']['#prefix'] = '<div data-toolbar="active" role="form" class="ckeditor-toolbar ckeditor-toolbar-active clearfix"><ul class="ckeditor-active-toolbar-configuration" role="presentation" aria-label="CKEditor toolbar and button configuration."><li class="ckeditor-row" role="group" aria-labelledby="ckeditor-active-toolbar"><ul class="ckeditor-toolbar-groups clearfix js-sortable"><li class="ckeditor-toolbar-group" role="presentation" data-drupal-ckeditor-type="group" data-drupal-ckeditor-toolbar-group-name="Embed button icon preview" tabindex="0"><h3 class="ckeditor-toolbar-group-name" id="ckeditor-toolbar-group-aria-label-for-formatting">Embed button icon preview</h3><ul class="ckeditor-buttons ckeditor-toolbar-group-buttons js-sortable" role="toolbar" data-drupal-ckeditor-button-sorting="target" aria-labelledby="ckeditor-toolbar-group-aria-label-for-formatting"><li data-drupal-ckeditor-button-name="Bold" class="ckeditor-button"><a href="#" role="button" title="' . $button
        ->label() . '" aria-label="' . $button
        ->label() . '"><span class="cke_button_icon">';
      $form['icon_preview']['image']['#suffix'] = '</span></a></li></ul></li></ul></div>';
      $form['icon_preview']['#attached']['library'][] = 'ckeditor/drupal.ckeditor.admin';
    }
  }
  return $form;
}