You are here

public function ParagraphsSetForm::form in Paragraphs Sets 8.2

Same name and namespace in other branches
  1. 8 src/Form/ParagraphsSetForm.php \Drupal\paragraphs_sets\Form\ParagraphsSetForm::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/ParagraphsSetForm.php, line 25

Class

ParagraphsSetForm
Form controller for paragraph set forms.

Namespace

Drupal\paragraphs_sets\Form

Code

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

  /** @var \Drupal\paragraphs_sets\ParagraphsSetInterface $paragraphs_set */
  $paragraphs_set = $this->entity;
  if (!$paragraphs_set
    ->isNew()) {
    $form['#title'] = $this
      ->t('Edit %title paragraph set', [
      '%title' => $paragraphs_set
        ->label(),
    ]);
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $paragraphs_set
      ->label(),
    '#description' => $this
      ->t("Label for the Paragraphs set."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $paragraphs_set
      ->id(),
    '#machine_name' => [
      'exists' => 'paragraphs_set_load',
    ],
    '#maxlength' => 32,
    '#disabled' => !$paragraphs_set
      ->isNew(),
  ];
  $form['icon_file'] = [
    '#title' => $this
      ->t('Paragraphs set icon'),
    '#type' => 'managed_file',
    '#upload_location' => 'public://paragraphs_set_icon/',
    '#upload_validators' => [
      'file_validate_extensions' => [
        'png jpg svg',
      ],
    ],
  ];
  if ($file = $this->entity
    ->getIconFile()) {
    $form['icon_file']['#default_value'] = [
      'target_id' => $file
        ->id(),
    ];
  }
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $paragraphs_set
      ->getDescription(),
  ];
  $paragraphs_config = '';
  if (!$paragraphs_set
    ->isNew()) {
    $config = $this
      ->config("paragraphs_sets.set.{$paragraphs_set->id()}");
    $paragraphs_config = Yaml::encode([
      'paragraphs' => $config
        ->get('paragraphs'),
    ]);
  }
  $form['paragraphs_config'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Paragraphs configuration'),
    '#description' => $this
      ->t('The paragraphs in the set and field default values can be defined in YAML syntax.'),
    '#default_value' => $paragraphs_config,
    '#rows' => 15,
    '#attributes' => [
      'data-yaml-editor' => 'true',
    ],
  ];
  if (!$this->moduleHandler
    ->moduleExists('yaml_editor')) {
    $form['paragraphs_config']['#description'] .= $this
      ->t('<br />For easier editing of the configuration consider installing the <a href="@yaml-editor">YAML Editor</a> module.', [
      '@yaml-editor' => 'https://www.drupal.org/project/yaml_editor',
    ]);
  }
  $config_example = [
    'paragraphs' => [
      [
        'type' => 'text_simple',
        'data' => [
          'field_headline' => 'Build something amazing with Drupal',
        ],
      ],
      [
        'type' => 'text',
        'data' => [
          'field_headline' => 'Paragraphs Sets',
          'field_content' => htmlentities('<p>You may also add some <strong>markup</strong> in the default value ...</p>'),
        ],
      ],
    ],
  ];
  $form['config_example'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Example configuration'),
  ];
  $form['config_example']['code'] = [
    '#prefix' => '<pre>',
    '#suffix' => '</pre>',
    '#markup' => Yaml::encode($config_example),
  ];
  return $form;
}