You are here

public function JsonapiResourceConfigForm::form in JSON:API Extras 8

Same name and namespace in other branches
  1. 8.3 src/Form/JsonapiResourceConfigForm.php \Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm::form()
  2. 8.2 src/Form/JsonapiResourceConfigForm.php \Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/JsonapiResourceConfigForm.php, line 132

Class

JsonapiResourceConfigForm
Base form for jsonapi_resource_config.

Namespace

Drupal\jsonapi_extras\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Disable caching on this form.
  $form_state
    ->setCached(FALSE);
  $entity_type_id = $this->request
    ->get('entity_type_id');
  $bundle = $this->request
    ->get('bundle');

  /** @var \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig $entity */
  $entity = $this
    ->getEntity();
  $resource_id = $entity
    ->get('id');

  // If we are editing an entity we don't want the Entity Type and Bundle
  // picker, that info is locked.
  if (!$entity_type_id || !$bundle) {
    if (!$resource_id) {

      // We can't build the form without an entity type and bundle.
      throw new \InvalidArgumentException('Unable to load entity type or bundle for the overrides form.');
    }
    list($entity_type_id, $bundle) = explode('--', $resource_id);
    $form['#title'] = $this
      ->t('Edit %label resource config', [
      '%label' => $resource_id,
    ]);
  }
  if ($entity_type_id && ($resource_type = $this->resourceTypeRepository
    ->get($entity_type_id, $bundle))) {

    // Get the JSON API resource type.
    $resource_config_id = sprintf('%s--%s', $entity_type_id, $bundle);
    $existing_entity = $this->entityTypeManager
      ->getStorage('jsonapi_resource_config')
      ->load($resource_config_id);
    if ($existing_entity && $entity
      ->isNew()) {
      drupal_set_message($this
        ->t('This override already exists, please edit it instead.'));
      return $form;
    }
    $form['bundle_wrapper']['fields_wrapper'] = $this
      ->buildOverridesForm($resource_type, $entity);
    $form['id'] = [
      '#type' => 'hidden',
      '#value' => sprintf('%s--%s', $entity_type_id, $bundle),
    ];
  }
  return $form;
}