BrightcovePlaylistForm.php in Brightcove Video Connect 8.2
File
src/Form/BrightcovePlaylistForm.php
View source
<?php
namespace Drupal\brightcove\Form;
use Brightcove\API\Exception\APIException;
use Drupal\brightcove\Entity\BrightcovePlaylist;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
class BrightcovePlaylistForm extends BrightcoveVideoPlaylistForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
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()) {
$form['api_client']['widget']['#ajax']['callback'] = [
self::class,
'apiClientUpdateForm',
];
$form['videos']['#ajax_id'] = 'ajax-videos-wrapper';
$form['videos']['#prefix'] = '<div id="' . $form['videos']['#ajax_id'] . '">';
$form['videos']['#suffix'] = '</div>';
}
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_type = array_keys(BrightcovePlaylist::getTypes(BrightcovePlaylist::TYPE_MANUAL));
$form['videos']['#states'] = [
'visible' => [
':input[name="type"]' => [
'value' => reset($manual_type),
],
],
];
$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;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
try {
$status = $entity
->save(TRUE);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this
->t('Created the %label Brightcove Playlist.', [
'%label' => $entity
->label(),
]));
break;
default:
drupal_set_message($this
->t('Saved the %label Brightcove Playlist.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.brightcove_playlist.canonical', [
'brightcove_playlist' => $entity
->id(),
]);
} catch (APIException $e) {
drupal_set_message($e
->getMessage(), 'error');
}
}
public static function apiClientUpdateForm(array $form, FormStateInterface $form_state) {
$response = parent::apiClientUpdateForm($form, $form_state);
foreach (Element::children($form['videos']['widget']) as $delta) {
if (is_numeric($delta)) {
$form['videos']['widget'][$delta]['target_id']['#value'] = '';
}
}
$response
->addCommand(new ReplaceCommand('#' . $form['videos']['#ajax_id'], $form['videos']));
return $response;
}
}