You are here

public function BrightcovePlaylistForm::buildForm in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 src/Form/BrightcovePlaylistForm.php \Drupal\brightcove\Form\BrightcovePlaylistForm::buildForm()
  2. 3.x src/Form/BrightcovePlaylistForm.php \Drupal\brightcove\Form\BrightcovePlaylistForm::buildForm()

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 BrightcoveVideoPlaylistForm::buildForm

File

src/Form/BrightcovePlaylistForm.php, line 21

Class

BrightcovePlaylistForm
Form controller for Brightcove Playlist edit forms.

Namespace

Drupal\brightcove\Form

Code

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

  /* @var $entity \Drupal\brightcove\Entity\BrightcovePlaylist */
  $entity = $this->entity;

  // Get api client from the form settings.
  if (!empty($api_client_value = $form_state
    ->getValue('api_client'))) {
    if (is_array($api_client_value)) {
      $api_client = $api_client_value[0]['target_id'];
    }
    else {
      $api_client = $api_client_value;
    }
  }
  elseif (!empty($user_input = $form_state
    ->getUserInput()) && isset($user_input['api_client'])) {
    $api_client = $user_input['api_client'];
  }
  else {
    $api_client = $form['api_client']['widget']['#default_value'];
    if (is_array($api_client)) {
      $api_client = reset($api_client);
    }
  }
  if ($entity
    ->isNew()) {

    // Class this class's update form method instead of the supper class's.
    $form['api_client']['widget']['#ajax']['callback'] = [
      self::class,
      'apiClientUpdateForm',
    ];

    // Add ajax wrapper for videos.
    $form['videos']['#ajax_id'] = 'ajax-videos-wrapper';
    $form['videos']['#prefix'] = '<div id="' . $form['videos']['#ajax_id'] . '">';
    $form['videos']['#suffix'] = '</div>';
  }

  // Set videos reference field argument.
  foreach (Element::children($form['videos']['widget']) as $delta) {
    if (is_numeric($delta)) {
      if (empty($form['videos']['widget'][$delta]['target_id']['#selection_settings']['view']['arguments'])) {
        $form['videos']['widget'][$delta]['target_id']['#selection_settings']['view']['arguments'] = [
          $api_client,
        ];
      }
    }
  }

  // Manual playlist: no search, only videos.
  $manual_type = array_keys(BrightcovePlaylist::getTypes(BrightcovePlaylist::TYPE_MANUAL));
  $form['videos']['#states'] = [
    'visible' => [
      ':input[name="type"]' => [
        'value' => reset($manual_type),
      ],
    ],
  ];

  // Smart playlist: no videos, only search.
  $smart_types = [];
  foreach (array_keys(BrightcovePlaylist::getTypes(BrightcovePlaylist::TYPE_SMART)) as $smart_type) {
    $smart_types[] = [
      'value' => $smart_type,
    ];
  }
  $form['tags_search_condition']['#states'] = [
    'visible' => [
      ':input[name="type"]' => $smart_types,
    ],
  ];
  $form['tags']['#states'] = [
    'visible' => [
      ':input[name="type"]' => $smart_types,
    ],
  ];
  return $form;
}