src/Form/RestUIForm.php | 59 ++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/Form/RestUIForm.php b/src/Form/RestUIForm.php index 189dbf2..bb7f2e4 100644 --- a/src/Form/RestUIForm.php +++ b/src/Form/RestUIForm.php @@ -109,6 +109,20 @@ class RestUIForm extends ConfigFormBase { } /** + * @param \Drupal\Core\Form\FormStateInterface $form_state + * @param $id + * @return array|mixed|null + */ + protected function getGranularity(FormStateInterface $form_state, $id) { + $granularity = $this->config("rest.resource.{$id}")->get('granularity'); + if ($form_state->hasValue('granularity')) { + // Form state takes priority over config. + $granularity = $form_state->getValue('granularity'); + } + return $granularity; + } + + /** * {@inheritdoc} * * @param array $form @@ -140,21 +154,13 @@ class RestUIForm extends ConfigFormBase { $form['resource_id'] = array('#type' => 'value', '#value' => $resource_id); $method_options = array_combine($methods, $methods); - $authentication_providers = array_keys( - $this->authenticationCollector->getSortedProviders() - ); - $authentication_providers = array_combine( - $authentication_providers, $authentication_providers - ); + $authentication_providers = array_keys($this->authenticationCollector->getSortedProviders()); + $authentication_providers = array_combine($authentication_providers, $authentication_providers); $format_options = array_combine($this->formats, $this->formats); - // Granularity selection. - $granularity = $this->config("rest.resource.{$id}")->get('granularity'); - if ($form_state->getValue('granularity')) { - // Form state takes priority over config. - $granularity = $form_state->getValue('granularity'); - } + $granularity = $this->getGranularity($form_state, $id); + // Granularity selection. $form['granularity'] = array( '#title' => t('Granularity'), '#type' => 'select', @@ -175,7 +181,7 @@ class RestUIForm extends ConfigFormBase { '#attributes' => array('id' => 'wrapper'), ); - if ($granularity == RestResourceConfigInterface::METHOD_GRANULARITY) { + if ($granularity === RestResourceConfigInterface::METHOD_GRANULARITY) { // Granularity is "Method". foreach ($methods as $method) { $group = array(); @@ -196,21 +202,17 @@ class RestUIForm extends ConfigFormBase { if (isset($config[$method]['supported_formats'])) { $enabled_formats = $config[$method]['supported_formats']; } + $method_checkbox_selector = ':input[name="wrapper[methods][' . $method . '][' . $method . ']"]'; + $states_show_if_method_is_enabled = array( + 'visible' => array($method_checkbox_selector=> array('checked' => TRUE)), + 'invisible' => array($method_checkbox_selector=> array('checked' => FALSE)), + ); $group['settings']['formats'] = array( '#type' => 'checkboxes', '#title' => $this->t('Accepted request formats'), '#options' => $format_options, '#default_value' => $enabled_formats, - '#states' => array( - 'visible' => array( - ':input[name="wrapper[methods][' . $method . '][' . $method - . ']"]' => array('checked' => TRUE), - ), - 'invisible' => array( - ':input[name="wrapper[methods][' . $method . '][' . $method - . ']"]' => array('checked' => FALSE), - ), - ), + '#states' => $states_show_if_method_is_enabled, ); // Authentication providers. @@ -223,16 +225,7 @@ class RestUIForm extends ConfigFormBase { '#type' => 'checkboxes', '#options' => $authentication_providers, '#default_value' => $enabled_auth, - '#states' => array( - 'visible' => array( - ':input[name="wrapper[methods][' . $method . '][' . $method - . ']"]' => array('checked' => TRUE), - ), - 'invisible' => array( - ':input[name="wrapper[methods][' . $method . '][' . $method - . ']"]' => array('checked' => FALSE), - ), - ), + '#states' => $states_show_if_method_is_enabled, ); $form['wrapper']['methods'][$method] = $group; }