You are here

public function FixedBlockContentForm::form in Fixed Block Content 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/FixedBlockContentForm.php, line 30

Class

FixedBlockContentForm
Fixed block content form.

Namespace

Drupal\fixed_block_content\Form

Code

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

  // We need at least one custom block type.
  $types = $this->entityTypeManager
    ->getStorage('block_content_type')
    ->loadMultiple();
  if (count($types) === 0) {
    return [
      '#markup' => $this
        ->t('You have not created any block types yet. Go to the <a href=":url">block type creation page</a> to add a new block type.', [
        ':url' => Url::fromRoute('block_content.type_add')
          ->toString(),
      ]),
    ];
  }
  $form = parent::form($form, $form_state);

  /** @var \Drupal\fixed_block_content\FixedBlockContentInterface $fixed_block */
  $fixed_block = $this->entity;
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#maxlength' => 255,
    '#default_value' => $fixed_block
      ->label(),
    '#description' => $this
      ->t("The block title."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $fixed_block
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => [
      'source' => [
        'title',
      ],
      'exists' => [
        'Drupal\\fixed_block_content\\Entity\\FixedBlockContent',
        'load',
      ],
    ],
    '#disabled' => !$fixed_block
      ->isNew(),
  ];

  // Block content type (bundle).
  $form['block_content_bundle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Block content'),
    '#description' => $this
      ->t('The block content type.'),
    '#options' => [],
    '#required' => TRUE,
    '#default_value' => $fixed_block
      ->getBlockContentBundle(),
  ];

  // Options.
  $form['options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Options'),
    '#open' => $fixed_block
      ->isNew(),
  ];

  // Protected option.
  $protected_description = $this
    ->t('When enabled, the standard custom block will not appear in the list of available blocks, being only available as a fixed block.');
  if (!$fixed_block
    ->isNew() && ($block_content = $fixed_block
    ->getBlockContent(FALSE))) {
    $protected_description = [
      [
        '#markup' => $protected_description,
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'br',
      ],
      [
        '#markup' => $this
          ->t('<em>This option cannot be changed because a custom block is already linked to this fixed block</em>.'),
      ],
    ];
  }
  $form['options']['protected'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Available only as fixed block'),
    '#description' => $protected_description,
    '#default_value' => $fixed_block
      ->isProtected(),
    '#disabled' => !empty($block_content),
  ];

  // Auto-export option.
  $form['options']['auto_export'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Automatic block content update'),
    '#description' => $this
      ->t('The automatic block content update takes place during the site configuration import.'),
    '#description_display' => 'before',
    '#default_value' => $fixed_block
      ->getAutoExportState(),
    '#options' => [
      FixedBlockContentInterface::AUTO_EXPORT_DISABLED => $this
        ->t('Disabled'),
      FixedBlockContentInterface::AUTO_EXPORT_ON_EMPTY => $this
        ->t('On empty'),
      FixedBlockContentInterface::AUTO_EXPORT_ALWAYS => $this
        ->t('Always'),
    ],
    FixedBlockContentInterface::AUTO_EXPORT_DISABLED => [
      '#description' => $this
        ->t('No action. No block content creation or update takes place.'),
    ],
    FixedBlockContentInterface::AUTO_EXPORT_ON_EMPTY => [
      '#description' => $this
        ->t("Create new block content, empty or with the default content, only if it doesn't exist."),
    ],
    FixedBlockContentInterface::AUTO_EXPORT_ALWAYS => [
      '#description' => [
        [
          '#markup' => $this
            ->t('Create new block content or update existing with the default content if it was changed.'),
        ],
        [
          '#type' => 'html_tag',
          '#tag' => 'br',
        ],
        [
          '#markup' => $this
            ->t('<em>Use with caution, any modifications in the in the custom block content will be lost.</em>'),
        ],
      ],
    ],
  ];

  /** @var \Drupal\block_content\Entity\BlockContentType $block_content_type */
  foreach ($types as $key => $block_content_type) {
    $form['block_content_bundle']['#options'][$key] = $block_content_type
      ->label();
  }
  return $form;
}