View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Form;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\entity_share_client\ClientAuthorization\ClientAuthorizationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RemoteForm extends EntityForm {
protected $authPluginManager;
protected $authPlugin;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->authPluginManager = $container
->get('plugin.manager.entity_share_client_authorization');
return $instance;
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$remote = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $remote
->label(),
'#description' => $this
->t('Label for the remote website.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $remote
->id(),
'#machine_name' => [
'source' => [
'label',
],
'exists' => '\\Drupal\\entity_share_client\\Entity\\Remote::load',
],
'#disabled' => !$remote
->isNew(),
];
$form['url'] = [
'#type' => 'url',
'#title' => $this
->t('URL'),
'#maxlength' => 255,
'#description' => $this
->t('The remote URL. Example: http://example.com'),
'#default_value' => $remote
->get('url'),
'#required' => TRUE,
];
$this
->addAuthOptions($form, $form_state);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!UrlHelper::isValid($form_state
->getValue('url'), TRUE)) {
$form_state
->setError($form['url'], $this
->t('Invalid URL.'));
}
$selectedPlugin = $this
->getSelectedPlugin($form, $form_state);
if ($selectedPlugin instanceof PluginFormInterface) {
$subformState = SubformState::createForSubform($form['auth']['data'], $form, $form_state);
$selectedPlugin
->validateConfigurationForm($form['auth']['data'], $subformState);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$selectedPlugin = $this
->getSelectedPlugin($form, $form_state);
$subformState = SubformState::createForSubform($form['auth']['data'], $form, $form_state);
$subformState
->set('remote', $this->entity);
$selectedPlugin
->submitConfigurationForm($form['auth']['data'], $subformState);
}
public function save(array $form, FormStateInterface $form_state) {
$remote = $this->entity;
if (!empty($form['auth']['#plugins'])) {
$selectedPlugin = $this
->getSelectedPlugin($form, $form_state);
$remote
->mergePluginConfig($selectedPlugin);
}
$status = $remote
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addStatus($this
->t('Created the %label remote website.', [
'%label' => $remote
->label(),
]));
break;
default:
$this
->messenger()
->addStatus($this
->t('Saved the %label remote website.', [
'%label' => $remote
->label(),
]));
}
$form_state
->setRedirectUrl($remote
->toUrl('collection'));
}
protected function addAuthOptions(array &$form, FormStateInterface $form_state) {
$options = [];
$plugins = [];
$commonUuid = '';
if ($this
->hasAuthPlugin()) {
$options[$this->authPlugin
->getPluginId()] = $this->authPlugin
->getLabel();
$plugins[$this->authPlugin
->getPluginId()] = $this->authPlugin;
$existing_plugin_configuration = $this->authPlugin
->getConfiguration();
$commonUuid = $existing_plugin_configuration['uuid'];
}
$availablePlugins = $this->authPluginManager
->getAvailablePlugins($commonUuid);
foreach ($availablePlugins as $id => $plugin) {
if (empty($options[$id])) {
$options[$id] = $plugin
->getLabel();
$plugins[$id] = $plugin;
}
}
$selected = $form_state
->getValue('pid');
if (!empty($selected)) {
$selectedPlugin = $plugins[$selected];
}
elseif (!empty($this->authPlugin)) {
$selectedPlugin = $this->authPlugin;
}
else {
$selectedPlugin = reset($plugins);
}
$form['auth'] = [
'#type' => 'container',
'#plugins' => $plugins,
'pid' => [
'#type' => 'radios',
'#title' => $this
->t('Authorization methods'),
'#options' => $options,
'#default_value' => $selectedPlugin
->getPluginId(),
'#ajax' => [
'wrapper' => 'plugin-form-ajax-container',
'callback' => [
get_class($this),
'ajaxPluginForm',
],
],
],
'data' => [],
];
$subformState = SubformState::createForSubform($form['auth']['data'], $form, $form_state);
$form['auth']['data'] = $selectedPlugin
->buildConfigurationForm($form['auth']['data'], $subformState);
$form['auth']['data']['#tree'] = TRUE;
$form['auth']['data']['#prefix'] = '<div id="plugin-form-ajax-container">';
$form['auth']['data']['#suffix'] = '</div>';
}
public static function ajaxPluginForm(array $form, FormStateInterface $formState) {
return $form['auth']['data'];
}
protected function hasAuthPlugin() {
$remote = $this->entity;
$plugin = $remote
->getAuthPlugin();
if ($plugin instanceof ClientAuthorizationInterface) {
$this->authPlugin = $plugin;
return TRUE;
}
return FALSE;
}
protected function getSelectedPlugin(array &$form, FormStateInterface $form_state) {
$authPluginId = $form_state
->getValue('pid');
$plugins = $form['auth']['#plugins'];
$selectedPlugin = $plugins[$authPluginId];
return $selectedPlugin;
}
}