diff --git a/README.md b/README.md index c58fe0d59063181f368300ffe32eb1a1e4c6cdf7..0cdb9fd3d6f1f295c16d95503699b667febde6f9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # JSON API Extras -This module provides extra functionality on top of JSON API. You should not need this module to get an spec compliant JSON API, this module is to customize the output of JSON API. +This module provides extra functionality on top of JSON API. You should not +need this module to get an spec compliant JSON API, this module is to +customize the output of JSON API. This module adds the following features: @@ -11,4 +13,5 @@ This module adds the following features: - Lets you remove fields from the JSON API output. TODO: - * Auto calculate the dependency of the provider of the entity type and bundles in the configuration entity. + * Auto calculate the dependency of the provider of the entity type and + bundles in the configuration entity. diff --git a/jsonapi_extras.module b/jsonapi_extras.module index f26e48740b18703b2f1cd377929f5a8f75bbaae5..ad0548587b9bffa61d2aaa323bac06e671d78420 100644 --- a/jsonapi_extras.module +++ b/jsonapi_extras.module @@ -15,7 +15,7 @@ function jsonapi_extras_help($route_name, RouteMatchInterface $route_match) { case 'entity.jsonapi_resource_config.collection': $output = ''; $output .= '

' . t('The following table shows the list of JSON API resources available.') . '

'; - $output .= '

' . t('Use the overwrite operation to overwrite a resource\'s configuration. You can revert back to the default configuration using the revert operation.') . '

'; + $output .= '

' . t("Use the overwrite operation to overwrite a resource's configuration. You can revert back to the default configuration using the revert operation.") . '

'; return $output; } } diff --git a/phpcs.xml.dist b/phpcs.xml similarity index 100% rename from phpcs.xml.dist rename to phpcs.xml diff --git a/src/Form/JsonapiExtrasSettingsForm.php b/src/Form/JsonapiExtrasSettingsForm.php index 98e3bb08cd35e6ecb08b8a88afec978daf628dbf..7fc5845a87b248394ff0a002ca0f844b62ed42c2 100644 --- a/src/Form/JsonapiExtrasSettingsForm.php +++ b/src/Form/JsonapiExtrasSettingsForm.php @@ -2,14 +2,42 @@ namespace Drupal\jsonapi_extras\Form; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RouteBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Configure JSON API settings for this site. */ class JsonapiExtrasSettingsForm extends ConfigFormBase { + protected $routerBuilder; + + /** + * Constructs a \Drupal\system\ConfigFormBase object. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The factory for configuration objects. + * @param \Drupal\Core\Routing\RouteBuilder $router_builder + * The router builder to rebuild menus after saving config entity. + */ + public function __construct(ConfigFactoryInterface $config_factory, RouteBuilder $router_builder) { + parent::__construct($config_factory); + $this->routerBuilder = $router_builder; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('router.builder') + ); + } + /** * {@inheritdoc} */ @@ -64,7 +92,7 @@ class JsonapiExtrasSettingsForm extends ConfigFormBase { ->save(); // Rebuild the router. - \Drupal::service('router.builder')->setRebuildNeeded(); + $this->routerBuilder->setRebuildNeeded(); parent::submitForm($form, $form_state); } diff --git a/src/Form/JsonapiResourceConfigForm.php b/src/Form/JsonapiResourceConfigForm.php index dc1f175dfb29b01f1b48bc4c6f4018103a39dcd5..a0c7f337506f6ad1802a29cfc44a8b41f47160d3 100644 --- a/src/Form/JsonapiResourceConfigForm.php +++ b/src/Form/JsonapiResourceConfigForm.php @@ -73,6 +73,8 @@ class JsonapiResourceConfigForm extends EntityForm { protected $request; /** + * The typed config manager. + * * @var \Drupal\Core\Config\TypedConfigManagerInterface */ protected $typedConfigManager; @@ -93,7 +95,9 @@ class JsonapiResourceConfigForm extends EntityForm { * @param \Drupal\Core\Config\ImmutableConfig $config * The config instance. * @param \Symfony\Component\HttpFoundation\Request $request + * The HTTP request. * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager + * The typed config manager. */ public function __construct(EntityTypeBundleInfoInterface $bundle_info, ResourceTypeRepository $resource_type_repository, EntityFieldManager $field_manager, EntityTypeRepositoryInterface $entity_type_repository, ResourceFieldEnhancerManager $enhancer_manager, ImmutableConfig $config, Request $request, TypedConfigManagerInterface $typed_config_manager) { $this->bundleInfo = $bundle_info; @@ -308,12 +312,12 @@ class JsonapiResourceConfigForm extends EntityForm { $markup = ''; $markup .= '
'; - $markup .= '
' . t('Disabled') . '
'; - $markup .= '
' . t('Check this if you want to disable this field completely. Disabling required fields will cause problems when writing to the resource.') . '
'; - $markup .= '
' . t('Alias') . '
'; - $markup .= '
' . t('Overrides the field name with a custom name. Example: Change "field_tags" to "tags".') . '
'; - $markup .= '
' . t('Enhancer') . '
'; - $markup .= '
' . t('Select an enhancer to manipulate the public output coming in and out.') . '
'; + $markup .= '
' . $this->t('Disabled') . '
'; + $markup .= '
' . $this->t('Check this if you want to disable this field completely. Disabling required fields will cause problems when writing to the resource.') . '
'; + $markup .= '
' . $this->t('Alias') . '
'; + $markup .= '
' . $this->t('Overrides the field name with a custom name. Example: Change "field_tags" to "tags".') . '
'; + $markup .= '
' . $this->t('Enhancer') . '
'; + $markup .= '
' . $this->t('Select an enhancer to manipulate the public output coming in and out.') . '
'; $markup .= '
'; $overrides_form['overrides']['fields']['info'] = [ '#markup' => $markup, @@ -488,7 +492,9 @@ class JsonapiResourceConfigForm extends EntityForm { $bundles = []; $bundle_info = $this->bundleInfo->getBundleInfo($entity_type_id); foreach ($bundle_info as $bundle_id => $info) { - $bundles[$bundle_id] = $info['translatable'] ? $this->t($info['label']) : $info['label']; + $bundles[$bundle_id] = $info['translatable'] + ? $this->t('%label', ['%label' => $info['label']]) + : $info['label']; } $form['bundle_wrapper']['_bundle_id'] = [ '#type' => 'select', diff --git a/src/JsonapiResourceConfigListBuilder.php b/src/JsonapiResourceConfigListBuilder.php index 12e89caecc85b2c3e3b0066cbaa320432039fd6b..5d22f41237a453c14348dc6f79037ea6b7840a0a 100644 --- a/src/JsonapiResourceConfigListBuilder.php +++ b/src/JsonapiResourceConfigListBuilder.php @@ -35,6 +35,7 @@ class JsonapiResourceConfigListBuilder extends ConfigEntityListBuilder { * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * @param \Drupal\Core\Entity\EntityStorageInterface $storage + * The storage. * @param \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceTypeRepository $resource_type_repository * The JSON API configurable resource type repository. * @param \Drupal\Core\Config\ImmutableConfig $config diff --git a/src/Normalizer/SchemataSchemaNormalizer.php b/src/Normalizer/SchemataSchemaNormalizer.php index f697b9b0f83b0fd21f53a629a7358d4837851864..247fa17394d65849e4bd63c2f3ebbb2f54b92004 100644 --- a/src/Normalizer/SchemataSchemaNormalizer.php +++ b/src/Normalizer/SchemataSchemaNormalizer.php @@ -18,6 +18,8 @@ class SchemataSchemaNormalizer extends SchemataJsonSchemaSchemataSchemaNormalize protected $resourceTypeRepository; /** + * Constructs a SchemataSchemaNormalizer object. + * * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepository $resource_type_repository * A resource repository. */ diff --git a/src/Plugin/Validation/Constraint/DuplicateFieldConstraint.php b/src/Plugin/Validation/Constraint/DuplicateFieldConstraint.php index cb1cd9bbba43b1b93c6369fb6e1d477c7efdeed8..1bd0ad7051ae009c284b5a111f31d0479f2ad582 100644 --- a/src/Plugin/Validation/Constraint/DuplicateFieldConstraint.php +++ b/src/Plugin/Validation/Constraint/DuplicateFieldConstraint.php @@ -5,6 +5,8 @@ namespace Drupal\jsonapi_extras\Plugin\Validation\Constraint; use Symfony\Component\Validator\Constraint; /** + * The constraint object. + * * @Constraint( * id = "jsonapi_extras__duplicate_field", * label = @Translation("Duplicate field", context = "Validation") @@ -12,6 +14,11 @@ use Symfony\Component\Validator\Constraint; */ class DuplicateFieldConstraint extends Constraint { + /** + * The error message for the constraint. + * + * @var string + */ public $message = 'The override must be unique.'; } diff --git a/src/Plugin/Validation/Constraint/DuplicateFieldConstraintValidator.php b/src/Plugin/Validation/Constraint/DuplicateFieldConstraintValidator.php index c1032de13378a83f7ae92d91a5aa67eec1ebd7da..657c91ef4a7293bff80343b56e41b08eee713ded 100644 --- a/src/Plugin/Validation/Constraint/DuplicateFieldConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/DuplicateFieldConstraintValidator.php @@ -7,10 +7,13 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; /** + * The validator. */ class DuplicateFieldConstraintValidator extends ConstraintValidator { /** + * The entity type manager. + * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; @@ -49,7 +52,8 @@ class DuplicateFieldConstraintValidator extends ConstraintValidator { ->addViolation(); } } - // Now compare the overrides with the default names to validate no dupes exist. + // Now compare the overrides with the default names to validate no dupes + // exist. foreach ($overrides as $field => $override) { if (array_key_exists($override, $resourceFields)) { $this->context->buildViolation($constraint->message) @@ -68,7 +72,10 @@ class DuplicateFieldConstraintValidator extends ConstraintValidator { } if ($resource_type->get('resourceType') == $entity_data['resourceType']) { - $this->context->buildViolation('There is already resource (@name) with this resource type.', ['@name' => $resource_type->id()]) + $this->context->buildViolation( + 'There is already resource (@name) with this resource type.', + ['@name' => $resource_type->id()] + ) ->atPath('resourceType') ->addViolation(); } diff --git a/src/ResourceType/ConfigurableResourceType.php b/src/ResourceType/ConfigurableResourceType.php index eb4aba336cde3f4110c95fcf458acbebde3a35ec..091754781e119f6d2476f4cb41a6f0a7595a4ebb 100644 --- a/src/ResourceType/ConfigurableResourceType.php +++ b/src/ResourceType/ConfigurableResourceType.php @@ -28,6 +28,8 @@ class ConfigurableResourceType extends ResourceType { protected $enhancerManager; /** + * The configuration factory. + * * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; diff --git a/src/ResourceType/ConfigurableResourceTypeRepository.php b/src/ResourceType/ConfigurableResourceTypeRepository.php index ae8b1c44d3de5700b16956ccebf46c0cd59da675..9c3cbad4ccbb910bf483a924534a7db28460c685 100644 --- a/src/ResourceType/ConfigurableResourceTypeRepository.php +++ b/src/ResourceType/ConfigurableResourceTypeRepository.php @@ -30,11 +30,15 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository { protected $enhancerManager; /** + * The configuration factory. + * * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; /** + * A list of resource types. + * * @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType[] */ protected $resourceTypes; diff --git a/tests/src/Functional/JsonExtrasApiFunctionalTest.php b/tests/src/Functional/JsonExtrasApiFunctionalTest.php index 7e60cbacc632422c00e857953cafe860953d1e56..3b612f2fc089a10e11206fb2a7c9901a0421c0ba 100644 --- a/tests/src/Functional/JsonExtrasApiFunctionalTest.php +++ b/tests/src/Functional/JsonExtrasApiFunctionalTest.php @@ -13,6 +13,8 @@ use Drupal\user\Entity\User; use Symfony\Component\Routing\Route; /** + * The test class for the main functionality. + * * @group jsonapi_extras */ class JsonExtrasApiFunctionalTest extends JsonApiFunctionalTestBase { @@ -59,12 +61,16 @@ class JsonExtrasApiFunctionalTest extends JsonApiFunctionalTestBase { // 1. Make sure the api root is under '/api' and not '/jsonapi'. /** @var \Symfony\Component\Routing\RouteCollection $route_collection */ - $route_collection = \Drupal::service('router.route_provider')->getRoutesByPattern('/api'); - $this->assertInstanceOf(Route::class, $route_collection->get('jsonapi.resource_list')); + $route_collection = \Drupal::service('router.route_provider') + ->getRoutesByPattern('/api'); + $this->assertInstanceOf( + Route::class, $route_collection->get('jsonapi.resource_list') + ); $this->drupalGet('/jsonapi'); $this->assertSession()->statusCodeEquals(404); - // 2. Make sure the count is included in collections. This also tests the overridden paths. + // 2. Make sure the count is included in collections. This also tests the + // overridden paths. $output = Json::decode($this->drupalGet('/api/articles')); $this->assertSame($num_articles, (int) $output['meta']['count']); $this->assertSession()->statusCodeEquals(200);