l10n_server-2.x-dev/l10n_server/src/ConfigurableConnectorPluginBase.php
l10n_server/src/ConfigurableConnectorPluginBase.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | <?php declare (strict_types=1); namespace Drupal\l10n_server; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginWithFormsTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use function array_intersect_key ; /** * Configurable connector plugin base class. */ abstract class ConfigurableConnectorPluginBase extends ConnectorPluginBase implements ConfigurableConnectorInterface { use PluginWithFormsTrait; /** * The connector manager service. * * @var \Drupal\l10n_server\ConnectorManager */ protected ConnectorManager $connectorManager ; /** * The connector object. * * @var \Drupal\l10n_server\ConnectorInterface */ protected ConnectorInterface $connector ; /** * Sets a config factory. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. */ public function setConfigFactory(ConfigFactoryInterface $config_factory ): void { $this ->configFactory = $config_factory ; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container , array $configuration , $plugin_id , $plugin_definition ): self { $instance = parent::create( $container , $configuration , $plugin_id , $plugin_definition ); $instance ->setConfigFactory( $container ->get( 'config.factory' )); $instance ->setConfiguration( $configuration ); return $instance ; } /** * {@inheritdoc} */ public function getConfiguration(): array { return $this ->configuration; } /** * {@inheritdoc} */ public function setConfiguration( array $configuration ): void { $this ->configuration = NestedArray::mergeDeep( $this ->defaultConfiguration(), $configuration ); } /** * {@inheritdoc} */ public function defaultConfiguration(): array { return []; } /** * {@inheritdoc} */ public function validateConfigurationForm( array & $form , FormStateInterface $form_state ): void { // Validation is optional. } /** * {@inheritdoc} */ public function submitConfigurationForm( array & $form , FormStateInterface $form_state ): void { $form_state ->cleanValues(); foreach ( array_intersect_key ( $form_state ->getValues(), $this ->configuration) as $config_key => $config_value ) { $this ->configuration[ $config_key ] = $config_value ; } } } |