You are here

public function MultipleUpdatesForm::buildForm in Scheduled Publish 8.3

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/MultipleUpdatesForm.php, line 28
Contains \Drupal\scheduled_publish\Form\MultipleUpdatesForm.

Class

MultipleUpdatesForm

Namespace

Drupal\scheduled_publish\Form

Code

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

  // Handle ajax form submissions.
  $this
    ->handleSubmissions($form_state);
  $wrapper = 'scheduled-publish-form-wrapper';
  $form['#tree'] = TRUE;
  $form['#prefix'] = '<div id="' . $wrapper . '">';
  $form['#suffix'] = '</div>';

  // CONTENT MODERATION WORKFLOWS
  $workflow_objects = $form_state
    ->get([
    'scheduled_publish',
    'workflow_objects',
  ]);
  if (!$workflow_objects) {
    $workflow_objects = Workflow::loadMultipleByType('content_moderation');
    $form_state
      ->set([
      'scheduled_publish',
      'workflow_objects',
    ], $workflow_objects);
  }
  if (!$workflow_objects) {
    $form['message'] = [
      '#theme_wrappers' => [
        'container',
      ],
      '#markup' => $this
        ->t('No content moderation workflows found.'),
    ];
    return $form;
  }

  // Warn users about how this works.
  $form['warning'] = [
    '#theme_wrappers' => [
      'container',
    ],
    '#markup' => $this
      ->t('Upon saving all existing scheduled publishing entries for the selected entities will be removed and new ones added.<br>The first entity is used to determine valid state changes. Invalid options for any following entities will not be saved.'),
  ];
  $workflows = [];
  foreach ($workflow_objects as $value) {
    $workflows[$value
      ->get('id')] = $value
      ->get('label');
  }
  $workflow = $form_state
    ->get([
    'scheduled_publish',
    'workflow',
  ]);
  if (!$workflow) {
    $workflow = key($workflows);
    $form_state
      ->set([
      'scheduled_publish',
      'workflow',
    ], $workflow);
  }
  $valid_entity_bundles = $workflow_objects[$workflow]
    ->get('type_settings');
  $valid_entity_bundles = $valid_entity_bundles['entity_types']['node'] ?? [];

  // Allow workflow selection if there's more than one.
  if (count($workflows) > 1) {
    $form['workflow'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Workflow'),
      '#options' => $workflows,
      '#description' => $this
        ->t('Entities will be limited to the selected workflow.'),
      '#name' => 'workflow',
      '#limit_validation_errors' => [
        [
          'workflow',
        ],
      ],
      '#default_value' => $workflow,
      '#ajax' => [
        'callback' => [
          get_called_class(),
          'getForm',
        ],
        'wrapper' => $wrapper,
      ],
    ];
  }

  // ENTITIES
  $form['entities'] = [
    '#type' => 'fieldset',
    '#title' => t('Content'),
  ];
  $entities = $form_state
    ->get([
    'scheduled_publish',
    'entities',
  ]);
  $entities_count = 0;
  if (!isset($entities)) {
    $entities = [];
    $form_state
      ->set([
      'scheduled_publish',
      'entities',
    ], $entities);
  }
  elseif ($entities_count = count($entities)) {
    foreach ($entities as $key => $value) {
      $form['entities']['entries'][$key] = [
        '#type' => 'entity_autocomplete',
        '#target_type' => 'node',
        '#selection_settings' => [
          'target_bundles' => $valid_entity_bundles,
        ],
        '#default_value' => $value,
      ];
    }
  }
  $form['entities']['entries'][$entities_count] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#selection_settings' => [
      'target_bundles' => $valid_entity_bundles,
    ],
  ];
  $form['entities']['save'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Select content'),
    '#name' => 'sp-entities-save',
    '#limit_validation_errors' => [
      [
        'entities',
      ],
    ],
    '#ajax' => [
      'callback' => [
        get_called_class(),
        'getForm',
      ],
      'wrapper' => $wrapper,
    ],
  ];

  // STATES
  $form['states'] = [
    '#type' => 'fieldset',
    '#title' => t('States'),
    '#element_validate' => [
      [
        get_class($this),
        'validateElement',
      ],
    ],
  ];

  // At least one entity is needed.
  if (!$entities) {
    $form['states']['message'] = [
      '#theme_wrappers' => [
        'container',
      ],
      '#markup' => $this
        ->t('Add at least one entity to be able to add status updates.'),
    ];
    return $form;
  }
  $states = $form_state
    ->get([
    'scheduled_publish',
    'states',
  ]);
  if (!isset($states)) {
    $states = [];
    $form_state
      ->set([
      'scheduled_publish',
      'states',
    ], $states);
  }
  elseif (count($states)) {
    $this
      ->addStates($form['states'], $form_state, $wrapper);
  }

  // Use the first entity to get moderation options.
  $entity = $form_state
    ->get([
    'scheduled_publish',
    'first_entity',
  ]);
  if (!$entity) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->load(reset($entities));
    $form_state
      ->set([
      'scheduled_publish',
      'first_entity',
    ], $entity);
  }
  $last_state = end($states);
  if ($last_state) {
    $orig_status = $entity->moderation_state->value;
    $entity->moderation_state->value = $last_state['state'];
  }
  $m_options = $this
    ->getModerationOptions($entity);
  if ($last_state) {
    $entity->moderation_state->value = $orig_status;
  }
  $form['states']['moderation_state'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Moderation state change'),
    '#options' => $m_options,
  ];
  $form['states']['value'] = [
    '#type' => 'datetime',
    '#title' => $this
      ->t('Scheduled date'),
    '#description' => $this
      ->t('The datetime of the scheduled publish'),
    '#date_increment' => 1,
    '#date_timezone' => date_default_timezone_get(),
  ];
  $form['states']['add'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Add status update'),
    '#name' => 'sp-state-add',
    '#limit_validation_errors' => [
      [
        'states',
      ],
    ],
    '#ajax' => [
      'callback' => [
        get_called_class(),
        'getForm',
      ],
      'wrapper' => $wrapper,
    ],
  ];
  $form['actions'] = [
    '#type' => 'container',
    '#weight' => 10,
  ];
  $form['actions']['save'] = [
    '#type' => 'submit',
    // Already validated data from form_state is saved.
    '#limit_validation_errors' => [],
    '#value' => $this
      ->t('Save'),
  ];
  return $form;
}