photos-6.0.x-dev/src/Form/PhotosMediaLibraryForm.php
src/Form/PhotosMediaLibraryForm.php
<?php
namespace Drupal\photos\Form;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\media_library\Form\AddFormBase;
use Drupal\photos\Plugin\media\Source\Photos;
/**
* Creates a form to create media entities from oEmbed URLs.
*
* @internal
* Form classes are internal.
*/
class PhotosMediaLibraryForm extends AddFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return $this->getBaseFormId() . '_photos';
}
/**
* {@inheritdoc}
*/
protected function getMediaType(FormStateInterface $form_state) {
if ($this->mediaType) {
return $this->mediaType;
}
$media_type = parent::getMediaType($form_state);
if ($media_type && !$media_type->getSource() instanceof Photos) {
throw new \InvalidArgumentException('Can only add media types which use a Photos source plugin.');
}
return $media_type;
}
/**
* {@inheritdoc}
*/
protected function buildInputElement(array $form, FormStateInterface $form_state) {
// Add a container to group the input elements for styling purposes.
$form['container'] = [
'#type' => 'container',
];
// @todo need custom autocomplete results.
// Include albums that user has access to and that have images.
// @todo allow any target bundles.
$form['container']['photos'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#title' => $this->t('Photo album'),
'#description' => $this->t('Photo album node.'),
'#required' => TRUE,
'#selection_handler' => 'default',
'#selection_settings' => [
'target_bundles' => ['photos'],
],
];
$form['container']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Add'),
'#button_type' => 'primary',
'#validate' => ['::validateUrl'],
'#submit' => ['::addButtonSubmit'],
'#ajax' => [
'callback' => '::updateFormCallback',
'wrapper' => 'media-library-wrapper',
// Add a fixed URL to post the form since AJAX forms are automatically
// posted to <current> instead of $form['#action'].
// @todo Remove when https://www.drupal.org/project/drupal/issues/2504115
// is fixed.
'url' => Url::fromRoute('media_library.ui'),
'options' => [
'query' => $this->getMediaLibraryState($form_state)->all() + [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
],
],
];
return $form;
}
/**
* Submit handler for the add button.
*
* @param array $form
* The form render array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function addButtonSubmit(array $form, FormStateInterface $form_state) {
$this->processInputValues([$form_state->getValue('photos')], $form, $form_state);
}
}
