Commit 366ba755 authored by Elliot Ward's avatar Elliot Ward
Browse files

Issue #3311240: New 2.0.x branch for PHP 8.1 / Drupal 9/10

parent 7e0398d9
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
  "keywords": ["Drupal"],
  "minimum-stability": "dev",
  "require": {
    "drupal/core": "^9 || ^10"
    "drupal/core": "^9 || ^10",
    "php": ">=8.1"
  }
}
+7 −13
Original line number Diff line number Diff line
@@ -9,33 +9,27 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class TypedResourceController.
 * Class to provide a controller for autocomplete look-ups.
 *
 * @package Drupal\jsonapi_reference\Controller
 */
class TypedResourceController extends ControllerBase {

  /**
   * Client to talk to our JSON:API entrypoint.
   *
   * @var \Drupal\jsonapi_reference\JsonApiClientInterface
   */
  protected $client;

  /**
   * TypedResourceController constructor.
   *
   * @param \Drupal\jsonapi_reference\JsonApiClientInterface $client
   *   Client to talk to our JSON:API entrypoint.
   */
  public function __construct(JsonApiClientInterface $client) {
    $this->client = $client;
  public function __construct(
    protected readonly JsonApiClientInterface $client,
  ) {
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
  public static function create(ContainerInterface $container): TypedResourceController {
    return new static($container->get('jsonapi_reference.jsonapi_client'));
  }

@@ -51,11 +45,11 @@ class TypedResourceController extends ControllerBase {
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   JsonResponse containing array of search results.
   *   Each element in the array conatins a value and label showing the matching
   *   Each element in the array contains a value and label showing the matching
   *   values of the autocomplete attribute, and the corresponding objects
   *   JSON:API ID.
   */
  public function autocomplete(Request $request, $resource_object_type, $autocomplete_attribute) {
  public function autocomplete(Request $request, string $resource_object_type, string $autocomplete_attribute): JsonResponse {
    $response = [];
    $query = $request->query->get('q');
    $search_results = $this->client->search($resource_object_type, $autocomplete_attribute, $query);
+5 −5
Original line number Diff line number Diff line
@@ -6,14 +6,14 @@ use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class JsonApiReferenceConfigForm.
 * Class to provide a form for configuring the module.
 */
class JsonApiReferenceConfigForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
  protected function getEditableConfigNames(): array {
    return [
      'jsonapi_reference.settings',
    ];
@@ -22,14 +22,14 @@ class JsonApiReferenceConfigForm extends ConfigFormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
  public function getFormId(): string {
    return 'json_api_reference_config_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $config = $this->config('jsonapi_reference.settings');
    $form['entrypoint'] = [
      '#type' => 'textfield',
@@ -61,7 +61,7 @@ class JsonApiReferenceConfigForm extends ConfigFormBase {
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    parent::submitForm($form, $form_state);

    $config = $this->config('jsonapi_reference.settings');
+15 −36
Original line number Diff line number Diff line
@@ -9,46 +9,26 @@ use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;

/**
 * Class JsonApiClient.
 * Client for accessing data through JSON:API.
 *
 * @package Drupal\jsonapi_reference
 */
class JsonApiClient implements JsonApiClientInterface {

  /**
   * Client for querying the JSON:API entrypoint.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Entrypoint to call to retrieve JSONAPI objects.
   *
   * @var string
   */
  protected $entrypoint;
  protected string $entrypoint;

  /**
   * Username to use when authenticating to the entrypoint.
   *
   * @var string
   */
  protected $username;
  protected string $username;

  /**
   * Password to use when authenticating to the entrypoint.
   *
   * @var string
   */
  protected $password;

  /**
   * Logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $loggerChannel;
  protected string $password;

  /**
   * JsonApiClient constructor.
@@ -60,18 +40,19 @@ class JsonApiClient implements JsonApiClientInterface {
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   Config factory.
   */
  public function __construct(ClientInterface $httpClient, LoggerChannelInterface $loggerChannel, ConfigFactoryInterface $configFactory) {
    $this->httpClient = $httpClient;
  public function __construct(
    protected readonly ClientInterface $httpClient,
    protected readonly LoggerChannelInterface $loggerChannel,
    ConfigFactoryInterface $configFactory) {
    $this->entrypoint = $configFactory->get('jsonapi_reference.settings')->get('endpoint');
    $this->username = $configFactory->get('jsonapi_reference.settings')->get('username');
    $this->password = $configFactory->get('jsonapi_reference.settings')->get('password');
    $this->loggerChannel = $loggerChannel;
  }

  /**
   * {@inheritdoc}
   */
  public function listResourceObjectTypes() {
  public function listResourceObjectTypes(): array {
    return array_keys($this->getResourceObjectTypes());
  }

@@ -83,7 +64,7 @@ class JsonApiClient implements JsonApiClientInterface {
   *   Each object contains a href element pointing to the JSON:API entrypoint
   *   for that specific type.
   */
  protected function getResourceObjectTypes() {
  protected function getResourceObjectTypes(): array {
    try {
      $response = $this->httpClient->request('get', $this->entrypoint, $this->getRequestOptions());
    }
@@ -102,7 +83,7 @@ class JsonApiClient implements JsonApiClientInterface {
  /**
   * {@inheritdoc}
   */
  public function search($resource_object_type, $search_attribute, $query) {
  public function search($resource_object_type, $search_attribute, $query): array {
    $path = $this->getPathForResourceObjectType($resource_object_type);
    if (!$path) {
      $this->loggerChannel->error('No path for resource object type @type found at JSON:API entrypoint @entrypoint.', [
@@ -149,7 +130,7 @@ class JsonApiClient implements JsonApiClientInterface {
  /**
   * {@inheritdoc}
   */
  public function searchById($resource_object_type, $id) {
  public function searchById(string $resource_object_type, string $id): object|NULL {
    $path = $this->getPathForResourceObjectType($resource_object_type);
    if (!$path) {
      return NULL;
@@ -163,10 +144,8 @@ class JsonApiClient implements JsonApiClientInterface {
      $this->loggerChannel->error($e->getMessage());
      return NULL;
    }
    $response = json_decode($response->getBody()->getContents());

    return $response;

    return json_decode($response->getBody()->getContents());
  }

  /**
@@ -175,7 +154,7 @@ class JsonApiClient implements JsonApiClientInterface {
   * @return array
   *   Array of request options, including headers and auth.
   */
  protected function getRequestOptions() {
  protected function getRequestOptions(): array {
    $options = [
      RequestOptions::HEADERS => [
        'Content-Type' => 'application/json',
@@ -202,7 +181,7 @@ class JsonApiClient implements JsonApiClientInterface {
   *   URI for the given object type.
   *   Empty string if none found.
   */
  protected function getPathForResourceObjectType($resource_object_type) {
  protected function getPathForResourceObjectType(string $resource_object_type): string {
    $types = $this->getResourceObjectTypes();

    if (!array_key_exists($resource_object_type, $types)) {
+5 −5
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
namespace Drupal\jsonapi_reference;

/**
 * Interface JsonApiClientInterface.
 * Interface for JSON:API clients.
 *
 * @package Drupal\jsonapi_reference
 */
@@ -22,7 +22,7 @@ interface JsonApiClientInterface {
   *     .
   *   ];
   */
  public function listResourceObjectTypes();
  public function listResourceObjectTypes(): array;

  /**
   * Search the entrypoint for a resource object of a given type.
@@ -39,13 +39,13 @@ interface JsonApiClientInterface {
   *   $search_attribute containing $query, and the values are the IDs of the
   *   corresponding resource objects.
   */
  public function search($resource_object_type, $search_attribute, $query);
  public function search(string $resource_object_type, string $search_attribute, string $query): array;

  /**
   * Looks up a resource object of a given type by ID.
   *
   * @param string $resource_object_type
   *   Object type, eg node--article.
   *   Object type, e.g. node--article.
   * @param string $id
   *   GUID of object we are searching for.
   *
@@ -58,6 +58,6 @@ interface JsonApiClientInterface {
   *   properties.
   *   Returns NULL if no object found.
   */
  public function searchById($resource_object_type, $id);
  public function searchById(string $resource_object_type, string $id): object|NULL;

}
Loading