You are here

public function RestUIForm::buildForm in REST UI 8

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

string $resource_id: A string that identifies the REST resource.

Return value

array The form structure.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException When no plugin found.

Overrides ConfigFormBase::buildForm

File

src/Form/RestUIForm.php, line 164

Class

RestUIForm
Provides a REST resource configuration form.

Namespace

Drupal\restui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $resource_id = NULL) {
  $plugin = $this->resourcePluginManager
    ->createInstance($resource_id);
  if (empty($plugin)) {
    throw new NotFoundHttpException();
  }
  $id = str_replace(':', '.', $resource_id);
  $config = $this
    ->config("rest.resource.{$id}")
    ->get('configuration') ?: [];
  $pluginDefinition = $plugin
    ->getPluginDefinition();
  $form['#title'] = $this
    ->t('Settings for resource %label', [
    '%label' => $pluginDefinition['label'],
  ]);
  $form['#tree'] = TRUE;
  $form['resource_id'] = [
    '#type' => 'value',
    '#value' => $resource_id,
  ];
  $authentication_providers = array_keys($this->authenticationCollector
    ->getSortedProviders());
  $authentication_providers = array_combine($authentication_providers, $authentication_providers);
  $format_options = array_combine($this->formats, $this->formats);
  $granularity = $this
    ->getGranularity($id, $form_state);

  // Granularity selection.
  $form['granularity'] = [
    '#title' => $this
      ->t('Granularity'),
    '#type' => 'select',
    '#options' => [
      RestResourceConfigInterface::RESOURCE_GRANULARITY => $this
        ->t('Resource'),
      RestResourceConfigInterface::METHOD_GRANULARITY => $this
        ->t('Method'),
    ],
    '#default_value' => $granularity,
    '#ajax' => [
      'callback' => '::processAjaxForm',
      'wrapper' => 'wrapper',
    ],
  ];

  // Wrapper for ajax callback.
  $form['wrapper'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'wrapper',
    ],
  ];
  $form['wrapper'] += $granularity === RestResourceConfigInterface::RESOURCE_GRANULARITY ? $this
    ->buildConfigurationFormForResourceGranularity($plugin, $authentication_providers, $format_options, $config) : $this
    ->buildConfigurationFormForMethodGranularity($plugin, $authentication_providers, $format_options, $config);
  return parent::buildForm($form, $form_state);
}