You are here

protected function RestUIForm::buildConfigurationFormForMethodGranularity in REST UI 8

Subform constructor when the selected granularity is 'method'.

Parameters

\Drupal\rest\Plugin\ResourceInterface $plugin: The REST Resource plugin being configured.

array $authentication_providers: All available authentication providers, to use for #options.

array $format_options: All available formats, to use for #options.

array $config: The current configuration for the REST Resource config entity, or the empty array if it does not yet exist.

Return value

array The subform structure.

1 call to RestUIForm::buildConfigurationFormForMethodGranularity()
RestUIForm::buildForm in src/Form/RestUIForm.php

File

src/Form/RestUIForm.php, line 229

Class

RestUIForm
Provides a REST resource configuration form.

Namespace

Drupal\restui\Form

Code

protected function buildConfigurationFormForMethodGranularity(ResourceInterface $plugin, array $authentication_providers, array $format_options, array $config) {
  $methods = $plugin
    ->availableMethods();
  $form = [];
  foreach ($methods as $method) {
    $group = [];
    $group[$method] = [
      '#title' => $method,
      '#type' => 'checkbox',
      '#default_value' => isset($config[$method]),
    ];
    $group['settings'] = [
      '#type' => 'container',
      '#attributes' => [
        'style' => 'padding-left:20px',
      ],
    ];

    // Available request formats.
    $enabled_formats = [];
    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 = [
      'visible' => [
        $method_checkbox_selector => [
          'checked' => TRUE,
        ],
      ],
      'invisible' => [
        $method_checkbox_selector => [
          'checked' => FALSE,
        ],
      ],
    ];
    $group['settings']['formats'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Accepted request formats'),
      '#options' => $format_options,
      '#default_value' => $enabled_formats,
      '#states' => $states_show_if_method_is_enabled,
    ];

    // Authentication providers.
    $enabled_auth = [];
    if (isset($config[$method]['supported_auth'])) {
      $enabled_auth = $config[$method]['supported_auth'];
    }
    $group['settings']['auth'] = [
      '#title' => $this
        ->t('Authentication providers'),
      '#type' => 'checkboxes',
      '#options' => $authentication_providers,
      '#default_value' => $enabled_auth,
      '#states' => $states_show_if_method_is_enabled,
    ];
    $form['methods'][$method] = $group;
  }
  return $form;
}