You are here

protected function PoolForm::poolForm in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 8 src/Form/PoolForm.php \Drupal\cms_content_sync\Form\PoolForm::poolForm()
  2. 2.0.x src/Form/PoolForm.php \Drupal\cms_content_sync\Form\PoolForm::poolForm()

Step 3: Select an existing pool or create a new one.

Return value

array the form

Throws

\Exception

1 call to PoolForm::poolForm()
PoolForm::form in src/Form/PoolForm.php
Gets the actual form array to be built.

File

src/Form/PoolForm.php, line 483

Class

PoolForm
Form handler for the Pool add and edit forms.

Namespace

Drupal\cms_content_sync\Form

Code

protected function poolForm(array $form, FormStateInterface $form_state) {
  $elements = $this
    ->syncCoreForm($form, $form_state, true);
  $elements['headline'] = [
    '#markup' => '<br><br><h1>Step 3: Pool properties</h1>',
  ];

  /**
   * @var \Drupal\cms_content_sync\Entity\Pool $pool
   */
  $pool = $this->entity;
  $default_name = $this
    ->getDefaults($form_state)['name'];
  $default_id = $this
    ->getDefaults($form_state)['id'];
  $options = $pool
    ->isNew() && empty($default_id) ? $this
    ->getRemotePools($form_state) : [];
  if (count($options) && !$form_state
    ->getValue('id')) {
    $elements['id'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Existing pools'),
      '#required' => true,
      '#default_value' => array_slice(array_keys($options), 0, 1)[0],
      '#options' => $options,
    ];
  }
  else {
    $elements['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => empty($default_name) ? $form_state
        ->getValue('label') ? $form_state
        ->getValue('label') : ($pool
        ->label() ? $pool
        ->label() : (empty(Pool::getAll()['content']) ? $this
        ->t('Content') : '')) : $default_name,
      '#description' => $this
        ->t("The pool name. If you aren't working with multiple pools just leave it as it is."),
      '#required' => true,
    ];
    $elements['id'] = [
      '#type' => 'machine_name',
      '#default_value' => empty($default_id) ? $form_state
        ->getValue('id') ? $form_state
        ->getValue('id') : ($pool
        ->id() ? $pool
        ->id() : (empty(Pool::getAll()['content']) ? 'content' : '')) : $default_id,
      '#machine_name' => [
        'exists' => [
          $this,
          'exist',
        ],
      ],
      '#description' => $this
        ->t("A unique ID that must be identical on all sites that want to connect to this pool. If you aren't working with multiple pools just leave it as it is."),
      '#disabled' => !$pool
        ->isNew(),
    ];
  }

  // AJAX? Show submit buttons inline.
  if ($form_state
    ->getTriggeringElement()) {
    $actions = $this
      ->actions($form, $form_state);
    $actions['submit']['#attributes']['class'][] = 'button--primary';
    $elements = array_merge($elements, $actions);
  }
  if (!isset($elements['label'])) {
    $elements['create'] = [
      '#prefix' => '<br><br>',
      '#type' => 'submit',
      '#submit' => [
        '::createNew',
      ],
      '#value' => $this
        ->t('Create new'),
      '#name' => 'create',
      '#ajax' => [
        'callback' => '::ajaxReturn',
        'wrapper' => self::CONTAINER_ID,
        'method' => 'replace',
        'effect' => 'fade',
        'progress' => [
          'type' => 'throbber',
          'message' => 'loading settings...',
        ],
      ],
    ];
  }
  return $elements;
}