You are here

function _jsonapi_defaults_form_alter in JSON:API Extras 8.3

Build JSON API Defaults part of the form.

Parameters

array $form: Drupal form array.

\Drupal\Core\Form\FormStateInterface $form_state: Drupal form_state object.

2 calls to _jsonapi_defaults_form_alter()
jsonapi_defaults_form_jsonapi_resource_config_add_form_alter in modules/jsonapi_defaults/jsonapi_defaults.module
Implements hook_form_FORM_ID_alter().
jsonapi_defaults_form_jsonapi_resource_config_edit_form_alter in modules/jsonapi_defaults/jsonapi_defaults.module
Implements hook_form_FORM_ID_alter().

File

modules/jsonapi_defaults/jsonapi_defaults.module, line 34
Contains module related hooks.

Code

function _jsonapi_defaults_form_alter(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig $config_resource */
  $config_resource = $form_state
    ->getFormObject()
    ->getEntity();
  $filters = _jsonapi_defaults_convert_value($config_resource
    ->getThirdPartySetting('jsonapi_defaults', 'default_filter'));
  $includes = $config_resource
    ->getThirdPartySetting('jsonapi_defaults', 'default_include');
  $sorting = _jsonapi_defaults_convert_value($config_resource
    ->getThirdPartySetting('jsonapi_defaults', 'default_sorting'));
  $form['bundle_wrapper']['fields_wrapper']['defaults'] = [
    '#type' => 'details',
    '#title' => t('Collection'),
    '#open' => $includes || $filters || $sorting,
    '#description' => t('Sets resource default parameters.'),
    '#states' => [
      'visible' => [
        ':input[name="disabled"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['bundle_wrapper']['fields_wrapper']['defaults']['default_include'] = [
    '#type' => 'textarea',
    '#title' => 'Default include list',
    '#default_value' => $includes ? implode("\n", $includes) : '',
    '#description' => t('Specify includes here (For example uid or field_image). Enter one include per line. If a request contains an "include" query string parameter this defaults will be ignored.'),
  ];
  $form['bundle_wrapper']['fields_wrapper']['defaults']['default_filter'] = [
    '#type' => 'textarea',
    '#title' => 'Default filter list',
    '#default_value' => $filters,
    '#description' => t('Enter one filter per line, in the format key=value. For example:<br />
        filter[titleFilter][condition][path]=title<br />
        filter[titleFilter][condition][value]=Comis Commodo Ymo<br />
        If a request contains an "filter" query string parameter those filters will be added to the defaults.'),
  ];
  $form['bundle_wrapper']['fields_wrapper']['defaults']['default_sorting'] = [
    '#type' => 'textarea',
    '#title' => 'Default sorting list',
    '#default_value' => $sorting,
    '#description' => t('Enter one sort per line, in the format key=value. For example:<br />
        sort[titleSort][path]=created<br />
        sort[titleSort][direction]=DESC<br />
        If a request contains an "sort" query string parameter those sorting will be added to the defaults.'),
  ];
  $form['bundle_wrapper']['fields_wrapper']['defaults']['page_limit'] = [
    '#type' => 'number',
    '#title' => 'Page limit',
    '#default_value' => $config_resource
      ->getThirdPartySetting('jsonapi_defaults', 'page_limit') ?: OffsetPage::SIZE_MAX,
    '#min' => OffsetPage::SIZE_MAX,
    '#description' => t('Enter the limit of records fetched per page. <strong>Please note that changing this can have an impact on performance and does have security implications.</strong><br />
      Read more in the JSON:API documentation at <a href="https://www.drupal.org/docs/8/core/modules/jsonapi-module/pagination#s--cant-i-set-a-page-limit-higher-than-50" target="_blank">drupal.org</a> and consider using the <a href="https://www.drupal.org/project/jsonapi_boost" target="_blank">JSON:API Boost module</a> for better performance.'),
  ];
  $form['#entity_builders'][] = 'jsonapi_defaults_form_jsonapi_resource_config_form_builder';
}