22
33namespace Drupal \graphql \Form ;
44
5+ use Drupal \Component \Plugin \ConfigurableInterface ;
56use Drupal \Component \Utility \UrlHelper ;
7+ use Drupal \Core \Ajax \AjaxResponse ;
8+ use Drupal \Core \Ajax \ReplaceCommand ;
69use Drupal \Core \Entity \EntityForm ;
710use Drupal \Core \Entity \EntityTypeInterface ;
811use Drupal \Core \Form \FormStateInterface ;
12+ use Drupal \Core \Form \SubformState ;
13+ use Drupal \Core \Plugin \PluginFormInterface ;
914use Drupal \Core \Routing \RequestContext ;
1015use Drupal \graphql \Plugin \SchemaPluginManager ;
1116use Symfony \Component \DependencyInjection \ContainerInterface ;
@@ -59,13 +64,33 @@ public static function create(ContainerInterface $container) {
5964 }
6065
6166 /**
62- * {@inheritdoc}
67+ * Ajax callback triggered by the type schema select element.
68+ *
69+ * @param array $form
70+ * The form array.
71+ *
72+ * @return \Drupal\Core\Ajax\AjaxResponse
73+ * The ajax response.
6374 */
64- public function form (array $ form , FormStateInterface $ formStaet ) {
65- $ form = parent ::form ($ form , $ formStaet );
75+ public function ajaxSchemaConfigurationForm (array $ form ) {
76+ $ response = new AjaxResponse ();
77+ $ response ->addCommand (new ReplaceCommand ('#edit-schema-configuration-plugin-wrapper ' , $ form ['schema_configuration ' ]));
6678
79+ return $ response ;
80+ }
81+
82+ /**
83+ * {@inheritdoc}
84+ */
85+ public function form (array $ form , FormStateInterface $ formState ) {
86+ $ form = parent ::form ($ form , $ formState );
6787 /** @var \Drupal\graphql\Entity\ServerInterface $server */
6888 $ server = $ this ->entity ;
89+ $ schemas = array_map (function ($ definition ) {
90+ return $ definition ['name ' ] ?? $ definition ['id ' ];
91+ }, $ this ->schemaManager ->getDefinitions ());
92+ $ schema = ($ formState ->getUserInput ()['schema ' ] ?: $ server ->get ('schema ' )) ?: reset (array_keys ($ schemas ));
93+
6994 if ($ this ->operation == 'add ' ) {
7095 $ form ['#title ' ] = $ this ->t ('Add server ' );
7196 }
@@ -96,13 +121,39 @@ public function form(array $form, FormStateInterface $formStaet) {
96121 $ form ['schema ' ] = [
97122 '#title ' => t ('Schema ' ),
98123 '#type ' => 'select ' ,
99- '#options ' => array_map (function ($ definition ) {
100- return $ definition ['name ' ] ?? $ definition ['id ' ];
101- }, $ this ->schemaManager ->getDefinitions ()),
102- '#default_value ' => $ server ->get ('schema ' ),
124+ '#options ' => $ schemas ,
125+ '#default_value ' => $ schema ,
103126 '#description ' => t ('The schema to use with this server. ' ),
127+ '#ajax ' => [
128+ 'callback ' => '::ajaxSchemaConfigurationForm ' ,
129+ 'progress ' => [
130+ 'type ' => 'throbber ' ,
131+ 'message ' => $ this ->t ('Updating schema configuration form. ' ),
132+ ],
133+ ],
134+ ];
135+
136+ $ form ['schema_configuration ' ] = [
137+ '#type ' => 'container ' ,
138+ '#prefix ' => '<div id="edit-schema-configuration-plugin-wrapper"> ' ,
139+ '#suffix ' => '</div> ' ,
140+ '#tree ' => TRUE ,
104141 ];
105142
143+ /* @var \Drupal\graphql\Plugin\SchemaPluginInterface $instance */
144+ $ instance = $ schema ? $ this ->schemaManager ->createInstance ($ schema ) : NULL ;
145+ if ($ instance instanceof PluginFormInterface && $ instance instanceof ConfigurableInterface) {
146+ $ instance ->setConfiguration ($ server ->get ('schema_configuration ' )[$ schema ] ?? []);
147+
148+ $ form ['schema_configuration ' ][$ schema ] = [
149+ '#type ' => 'fieldset ' ,
150+ '#title ' => $ this ->t ('Schema configuration ' ),
151+ '#tree ' => TRUE ,
152+ ];
153+
154+ $ form ['schema_configuration ' ][$ schema ] += $ instance ->buildConfigurationForm ([], $ formState );
155+ }
156+
106157 $ form ['endpoint ' ] = [
107158 '#title ' => t ('Endpoint ' ),
108159 '#type ' => 'textfield ' ,
@@ -148,6 +199,8 @@ public function form(array $form, FormStateInterface $formStaet) {
148199
149200 /**
150201 * {@inheritdoc}
202+ *
203+ * @throws \Drupal\Component\Plugin\Exception\PluginException
151204 */
152205 public function validateForm (array &$ form , FormStateInterface $ formState ) {
153206 $ endpoint = &$ formState ->getValue ('endpoint ' );
@@ -158,13 +211,35 @@ public function validateForm(array &$form, FormStateInterface $formState) {
158211 if ($ endpoint [0 ] !== '/ ' ) {
159212 $ formState ->setErrorByName ('endpoint ' , 'The endpoint path has to start with a forward slash. ' );
160213 }
161- else if (!UrlHelper::isValid ($ endpoint )) {
214+ elseif (!UrlHelper::isValid ($ endpoint )) {
162215 $ formState ->setErrorByName ('endpoint ' , 'The endpoint path contains invalid characters. ' );
163216 }
217+
218+ /* @var \Drupal\graphql\Plugin\SchemaPluginInterface $instance */
219+ $ schema = $ formState ->getValue ('schema ' );
220+ $ instance = $ this ->schemaManager ->createInstance ($ schema );
221+ if (!empty ($ form ['schema_configuration ' ][$ schema ]) && $ instance instanceof PluginFormInterface && $ instance instanceof ConfigurableInterface) {
222+ $ state = SubformState::createForSubform ($ form ['schema_configuration ' ][$ schema ], $ form , $ formState );
223+ $ instance ->validateConfigurationForm ($ form ['schema_configuration ' ][$ schema ], $ state );
224+ }
225+ }
226+
227+ public function submitForm (array &$ form , FormStateInterface $ formState ) {
228+ parent ::submitForm ($ form , $ formState );
229+
230+ /* @var \Drupal\graphql\Plugin\SchemaPluginInterface $instance */
231+ $ schema = $ formState ->getValue ('schema ' );
232+ $ instance = $ this ->schemaManager ->createInstance ($ schema );
233+ if ($ instance instanceof PluginFormInterface && $ instance instanceof ConfigurableInterface) {
234+ $ state = SubformState::createForSubform ($ form ['schema_configuration ' ][$ schema ], $ form , $ formState );
235+ $ instance ->submitConfigurationForm ($ form ['schema_configuration ' ][$ schema ], $ state );
236+ }
164237 }
165238
166239 /**
167240 * {@inheritdoc}
241+ *
242+ * @throws \Drupal\Component\Plugin\Exception\PluginException
168243 */
169244 public function save (array $ form , FormStateInterface $ formState ) {
170245 parent ::save ($ form , $ formState );
0 commit comments