jsonapi_defaults.module in JSON:API Extras 8.3
Contains module related hooks.
File
modules/jsonapi_defaults/jsonapi_defaults.moduleView source
<?php
/**
* @file
* Contains module related hooks.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\jsonapi\Query\OffsetPage;
use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig;
/**
* Implements hook_form_FORM_ID_alter().
*/
function jsonapi_defaults_form_jsonapi_resource_config_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_jsonapi_defaults_form_alter($form, $form_state);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function jsonapi_defaults_form_jsonapi_resource_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_jsonapi_defaults_form_alter($form, $form_state);
}
/**
* Build JSON API Defaults part of the form.
*
* @param array $form
* Drupal form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Drupal form_state object.
*/
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';
}
/**
* Entity builder for json api resource configuration entity.
*/
function jsonapi_defaults_form_jsonapi_resource_config_form_builder($entity_type, JsonapiResourceConfig $config_resource, &$form, FormStateInterface $form_state) {
if ($raw_value = $form_state
->getValue('default_filter')) {
$value = _jsonapi_defaults_convert_rawvalue($raw_value);
$config_resource
->setThirdPartySetting('jsonapi_defaults', 'default_filter', $value);
}
else {
$config_resource
->unsetThirdPartySetting('jsonapi_defaults', 'default_filter');
}
if ($raw_value = $form_state
->getValue('default_sorting')) {
$value = _jsonapi_defaults_convert_rawvalue($raw_value);
$config_resource
->setThirdPartySetting('jsonapi_defaults', 'default_sorting', $value);
}
else {
$config_resource
->unsetThirdPartySetting('jsonapi_defaults', 'default_sorting');
}
if ($raw_value = $form_state
->getValue('default_include')) {
$include = array_map('trim', preg_split('/\\r\\n?|\\n/', $raw_value));
$include = array_filter($include);
$config_resource
->setThirdPartySetting('jsonapi_defaults', 'default_include', $include);
}
else {
$config_resource
->unsetThirdPartySetting('jsonapi_defaults', 'default_include');
}
if ($page_limit = $form_state
->getValue('page_limit')) {
$config_resource
->setThirdPartySetting('jsonapi_defaults', 'page_limit', $page_limit);
}
else {
$config_resource
->unsetThirdPartySetting('jsonapi_defaults', 'page_limit');
}
}
/**
* Convert default parameters value to array (used in config).
*
* @param string|null $raw_value
* Raw value from textarea.
*
* @return array
* Value to be saved into config.
*/
function _jsonapi_defaults_convert_rawvalue($raw_value) {
$value = [];
$raw_value = !is_string($raw_value) ? '' : $raw_value;
foreach (preg_split('/\\r\\n|[\\r\\n]/', $raw_value) as $param) {
if (strpos($param, '=') !== FALSE) {
$a = explode('=', $param);
$key = $a[0];
$val = $a[1];
if ($key == 'include') {
$value[$key] = trim($val);
}
elseif (preg_match('/^filter.+/', $key)) {
$key = str_replace('filter[', '', $key);
$key = explode('][', $key);
$value['filter:' . trim(implode('#', $key), '[]')] = $val;
}
elseif (preg_match('/^sort.+/', $key)) {
$key = str_replace('sort[', '', $key);
$key = explode('][', $key);
$value['sort:' . trim(implode('#', $key), '[]')] = $val;
}
}
}
return $value;
}
/**
* Convert default parameters value to raw value (textarea).
*
* @param array|null $value
* Value from config.
*
* @return string
* Value to be shown in textarea.
*/
function _jsonapi_defaults_convert_value($value) {
$raw_value = '';
if (is_array($value)) {
foreach ($value as $key => $val) {
if ($key == 'include') {
$raw_value .= "{$key}={$val}\n";
}
elseif (preg_match('/^filter.+/', $key)) {
$key = implode('][', explode('#', preg_replace('/^filter:/', '', $key)));
$raw_value .= "filter[{$key}]={$val}\n";
}
elseif (preg_match('/^sort.+/', $key)) {
$key = implode('][', explode('#', preg_replace('/^sort:/', '', $key)));
$raw_value .= "sort[{$key}]={$val}\n";
}
}
}
return $raw_value;
}
Functions
Name | Description |
---|---|
jsonapi_defaults_form_jsonapi_resource_config_add_form_alter | Implements hook_form_FORM_ID_alter(). |
jsonapi_defaults_form_jsonapi_resource_config_edit_form_alter | Implements hook_form_FORM_ID_alter(). |
jsonapi_defaults_form_jsonapi_resource_config_form_builder | Entity builder for json api resource configuration entity. |
_jsonapi_defaults_convert_rawvalue | Convert default parameters value to array (used in config). |
_jsonapi_defaults_convert_value | Convert default parameters value to raw value (textarea). |
_jsonapi_defaults_form_alter | Build JSON API Defaults part of the form. |