|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2web_datalookup\Form; |
| 4 | + |
| 5 | +use Drupal\Component\Plugin\PluginManagerInterface; |
| 6 | +use Drupal\Core\Form\ConfigFormBase; |
| 7 | +use Drupal\Core\Form\FormStateInterface; |
| 8 | +use Drupal\Core\Link; |
| 9 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Form or configuring AuthProvider plugin. |
| 13 | + */ |
| 14 | +class DataLookupPluginGroupSettingsForm extends ConfigFormBase { |
| 15 | + |
| 16 | + /** |
| 17 | + * Name of the config. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + public static $configName = 'os2web_datalookup.groups.settings'; |
| 22 | + |
| 23 | + /** @var \Drupal\os2web_datalookup\Plugin\DataLookupManager */ |
| 24 | + protected $dataLookupManager; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructs a new DataLookupPluginGroupSettingsForm object. |
| 28 | + * |
| 29 | + * @param \Drupal\os2web_datalookup\Plugin\DataLookupManager |
| 30 | + * Datalookup manager. |
| 31 | + */ |
| 32 | + public function __construct(PluginManagerInterface $dataLookupManager) { |
| 33 | + $this->dataLookupManager = $dataLookupManager; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public static function create(ContainerInterface $container) { |
| 40 | + return new static( |
| 41 | + $container->get('plugin.manager.os2web_datalookup') |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritdoc} |
| 47 | + */ |
| 48 | + public function getFormId() { |
| 49 | + return 'os2web_datalookop_group_settings_form_' . $this->getGroupIdFromRequest(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + protected function getEditableConfigNames() { |
| 56 | + return [DataLookupPluginGroupSettingsForm::$configName]; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the value of the param _plugin_id for the current request. |
| 61 | + * |
| 62 | + * @see \Drupal\os2web_nemlogin\Routing\AuthProviderRoutes |
| 63 | + */ |
| 64 | + protected function getGroupIdFromRequest() { |
| 65 | + $request = $this->getRequest(); |
| 66 | + return $request->get('_group_id'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritdoc} |
| 71 | + */ |
| 72 | + public function buildForm(array $form, FormStateInterface $form_state) { |
| 73 | + $group_id = $this->getGroupIdFromRequest(); |
| 74 | + |
| 75 | + $plugin_definitions = $this->dataLookupManager->getDefinitionsByGroup($group_id); |
| 76 | + |
| 77 | + $header = [ |
| 78 | + 'title' => $this |
| 79 | + ->t('Title'), |
| 80 | + 'status' => $this |
| 81 | + ->t('Status'), |
| 82 | + 'action' => $this |
| 83 | + ->t('Actions'), |
| 84 | + ]; |
| 85 | + |
| 86 | + $options = []; |
| 87 | + foreach ($plugin_definitions as $id => $plugin_definition) { |
| 88 | + /** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterface $plugin */ |
| 89 | + $plugin = $this->dataLookupManager->createInstance($id); |
| 90 | + |
| 91 | + $status = $plugin->getStatus(); |
| 92 | + |
| 93 | + $options[$plugin_definition['id']] = [ |
| 94 | + 'title' => $plugin_definition['label'], |
| 95 | + 'status' => ($plugin->isReady() ? $this->t('READY') : $this->t('ERROR')) . ': ' . $status, |
| 96 | + 'group' => $plugin_definition['group'], |
| 97 | + 'action' => Link::createFromRoute($this->t('Settings'), "os2web_datalookup.$id"), |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + $form['default_plugin'] = [ |
| 102 | + '#type' => 'tableselect', |
| 103 | + '#header' => $header, |
| 104 | + '#options' => $options, |
| 105 | + '#multiple' => FALSE, |
| 106 | + '#default_value' => $this->dataLookupManager->getGroupDefaultPlugin($group_id) ?? NULL, |
| 107 | + '#empty' => $this |
| 108 | + ->t('No plugins found'), |
| 109 | + ]; |
| 110 | + |
| 111 | + return parent::buildForm($form, $form_state); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * {@inheritdoc} |
| 116 | + */ |
| 117 | + public function submitForm(array &$form, FormStateInterface $form_state) { |
| 118 | + $group_id = $this->getGroupIdFromRequest(); |
| 119 | + |
| 120 | + $config = $this->config(DataLookupPluginGroupSettingsForm::$configName); |
| 121 | + |
| 122 | + $config->set("$group_id.default_plugin", $form_state->getValue('default_plugin')); |
| 123 | + $config->save(); |
| 124 | + |
| 125 | + parent::submitForm($form, $form_state); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments