You are here

public function TemplateForm::form in Wysiwyg API template plugin 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/TemplateForm.php \Drupal\wysiwyg_template\Form\TemplateForm::form()

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/TemplateForm.php, line 21

Class

TemplateForm
Class TemplateForm.

Namespace

Drupal\wysiwyg_template\Form

Code

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

  /** @var \Drupal\wysiwyg_template_core\TemplateInterface $wysiwyg_template */
  $wysiwyg_template = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#maxlength' => 255,
    '#default_value' => $wysiwyg_template
      ->label(),
    '#description' => $this
      ->t('Select a name for this template.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $wysiwyg_template
      ->id(),
    '#machine_name' => array(
      'exists' => '\\Drupal\\wysiwyg_template\\Entity\\Template::load',
    ),
    '#disabled' => !$wysiwyg_template
      ->isNew(),
  ];
  $form['description'] = [
    '#type' => 'textfield',
    '#default_value' => $wysiwyg_template
      ->getDescription(),
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('A description to be shown with the template.'),
  ];
  $form['body'] = [
    '#type' => 'text_format',
    '#format' => $wysiwyg_template
      ->getFormat(),
    '#default_value' => $wysiwyg_template
      ->getBody(),
    '#title' => $this
      ->t('HTML template'),
    '#rows' => 10,
    '#required' => TRUE,
  ];
  $form['entity_types'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Available for:'),
    '#description' => $this
      ->t('If you select no type in a section, this template will be available for all types of that section.'),
    '#tree' => TRUE,
  ];
  foreach (Drupal::service('wysiwyg_template.services')
    ->getEntityTypesAndBundles() as $id => $defintion) {
    if (!empty($defintion['bundles'])) {
      $types = [];
      foreach ($defintion['bundles'] as $bundleId => $bundeDef) {
        $types[$bundleId] = $bundeDef['label'];
      }
      $form['entity_types'][$id] = [
        '#type' => 'checkboxes',
        '#default_value' => $wysiwyg_template
          ->getBundles($id),
        '#title' => $defintion['label'],
        '#options' => $types,
      ];
    }
  }
  return $form;
}