You are here

public function CkeditorTemplateForm::form in Ckeditor templates user interface 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/CkeditorTemplateForm.php, line 44

Class

CkeditorTemplateForm
Implement config form for Ckeditor template.

Namespace

Drupal\ckeditor_templates_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $template = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $template
      ->label(),
    '#description' => $this
      ->t('Your Template title'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $template
      ->id(),
    '#machine_name' => [
      'exists' => [
        $this,
        'exist',
      ],
    ],
    '#disabled' => !$template
      ->isNew(),
    '#required' => TRUE,
  ];
  $form['description'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Description'),
    '#default_value' => $template
      ->getDescription(),
    '#description' => $this
      ->t('Your Template description'),
  ];
  $image = $template
    ->get('image');
  if ($image) {
    $image_markup = '<div class="form-item image-preview" style="max-width: 200px; max-height: 200px;">';
    $image_markup .= '<img src="' . file_create_url($image) . '" alt="' . $this
      ->t('Preview') . '" />';
    $image_markup .= '</div>';
    $form['image_preview'] = [
      '#type' => 'inline_template',
      '#template' => $image_markup,
    ];
  }
  $form['image'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Image path for this template'),
    '#default_value' => $image,
    '#description' => $this
      ->t('Examples: public://test.png, modules/my_module/test.png, themes/my_theme/test.png, //example.com/test.jpg'),
  ];
  $form['image_upload'] = [
    '#title' => $this
      ->t('Upload image for this template'),
    '#type' => 'file',
    '#description' => $this
      ->t('You can use this field if you need to upload the file to the server. Allowed extensions: gif png jpg jpeg.'),
    '#upload_validators' => [
      'file_validate_is_image' => [],
      'file_validate_extensions' => [
        'gif png jpg jpeg',
      ],
      'file_validate_size' => [
        25600000,
      ],
    ],
  ];
  $form['html'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Body'),
    '#description' => $this
      ->t('The predefined ckeditor template body'),
    '#required' => TRUE,
  ];
  if (!$template
    ->isNew()) {
    $form['html']['#format'] = $template
      ->getHtml()['format'];
    $form['html']['#default_value'] = $template
      ->getHtml()['value'];
  }
  return $form;
}