Skip to content
Snippets Groups Projects

Introduce "adapters"

Merged
Feliksas Mazeikisrequested to merge
research__adapters_poc into 0.x
5 open threads

Goals

  • Being able to write adaptors that can perform BE logic and adapt Drupal field values to match props

Non-goals

  • Good UX
  • Any UI to change the expression used for a sourceType == dynamic prop value
  • Nested adapters
  • Translations
  • API representation (JSON:API) or otherwise

MR contents

  • ** New Adapter plugin type and AdapterManager service **. Each plugin can:
    1. addInput(string $input, mixed $itemBase) - adds input to adapter instance
    2. adapt() - adapts output
    3. outputSchemaMatch($schema) - checks if $schema matches output schema of the adaptor

Tests (minimal)

  • :white_check_mark: Kernel
Edited by Wim Leers

Merge request reports

Merge train pipeline #187386 passed

Merge train pipeline passed for a0ad51d8

Approved by

Merged by Feliksas MazeikisFeliksas Mazeikis May 31, 2024 (May 31, 2024 3:20pm UTC)

Merge details

  • Changes merged into 0.x with a0ad51d8 (commits were squashed).
  • Deleted the source branch.
  • Auto-merge enabled

Pipeline #189532 failed

Pipeline failed for a0ad51d8 on 0.x

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • added 1 commit

    • 1c2a4a4e - Apply 1 suggestion(s) to 1 file(s)

    Compare with previous version

  • added 1 commit

    Compare with previous version

  • added 1 commit

    Compare with previous version

  • added 1 commit

    • 724b2f70 - finding adapters to match sdc props by output schema

    Compare with previous version

  • Feliksas Mazeikis resolved all threads

    resolved all threads

  • added 1 commit

    • 3728660f - Removing redundant assertion

    Compare with previous version

  • added 1 commit

    Compare with previous version

  • added 1 commit

    • 13a5af82 - Improving test, minor refactor

    Compare with previous version

  • added 1 commit

    • 9d30b703 - phpstan, phpcs and general cleanup related refactoring

    Compare with previous version

  • Feliksas Mazeikis marked this merge request as ready

    marked this merge request as ready

  • added 1 commit

    Compare with previous version

  • Wim Leers added 4 commits

    added 4 commits

    Compare with previous version

  • Wim Leers changed title from Research adapters poc to Introduce "adapters"

    changed title from Research adapters poc to Introduce "adapters"

  • Wim Leers added 1 commit

    added 1 commit

    • 281806d6 - Explicit sorting — plugin discovery orders is not guaranteed to have consistent ordering.

    Compare with previous version

  • Wim Leers added 2 commits

    added 2 commits

    • 86ffc20f - Define a `JsonSchema` PHPStan alias, this greatly improves clarity and would...
    • 714072f6 - The `output` of an adapter should not be a string, but a JSON schema array.

    Compare with previous version

  • 12 *
    13 * Plugin Namespace: Plugin\Adapter
    14 *
    15 * @see \Drupal\experience_builder\Plugin\Adapters\AdapterInterface
    16 * @see \Drupal\experience_builder\Plugin\AdapterManager
    17 * @see plugin_api
    18 */
    19 #[\Attribute(\Attribute::TARGET_CLASS)]
    20 class Adapter extends Plugin {
    21
    22 /**
    23 * @param string $id
    24 * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
    25 * @param array<string, mixed> $inputs
    26 * @param array<string> $requiredInputs
    27 * @param string $output
  • Wim Leers
  • Wim Leers
  • 57 $message = implode("/n", $message_parts);
    58 throw new \Exception($message);
    59 }
    60
    61 /**
    62 * @return array<string, mixed>
    63 */
    64 public function getOutputSchema(): array {
    65 assert(is_array($this->getPluginDefinition()));
    66 assert(array_key_exists('output', $this->getPluginDefinition()));
    67 return json_decode((string) file_get_contents($this->getPluginDefinition()['output']), TRUE);
    68 }
    69
    70 /**
    71 * @todo: is there a better way?
    72 */
  • Wim Leers
  • Wim Leers
  • Wim Leers
  • Wim Leers
  • Wim Leers
  • Wim Leers
  • Wim Leers added 1 commit

    added 1 commit

    Compare with previous version

  • 12 *
    13 * Plugin Namespace: Plugin\Adapter
    14 *
    15 * @see \Drupal\experience_builder\Plugin\Adapters\AdapterInterface
    16 * @see \Drupal\experience_builder\Plugin\AdapterManager
    17 * @see plugin_api
    18 *
    19 * @phpstan-import-type JsonSchema from \Drupal\experience_builder\SdcPropJsonSchemaType
    20 */
    21 #[\Attribute(\Attribute::TARGET_CLASS)]
    22 class Adapter extends Plugin {
    23
    24 /**
    25 * @param string $id
    26 * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
    27 * @param array<string, array<string, mixed>> $inputs
  • Wim Leers added 2 commits

    added 2 commits

    • 10077575 - Make `::getInputSchema()` more robust and add return type to `::getInputs()`.
    • 4133523e - Fix YAML syntax error.

    Compare with previous version

  • Wim Leers added 1 commit

    added 1 commit

    • adf28671 - Remove boilerplate docs to satisfy PHPStan while it is caused by upstream — we...

    Compare with previous version

  • Wim Leers added 2 commits

    added 2 commits

    • e4760c96 - My fixing a batch of nits introduced a failure when *using* adapters. :see_no_evil:
    • 7746b9ac - Make the `image-uri` JSON schema definition more robust: allow query strings and fragments.

    Compare with previous version

  • Wim Leers added 1 commit

    added 1 commit

    • 71b25b44 - Ignore the PHPStan errors triggered for `ContainerFactoryPluginInterface`...

    Compare with previous version

  • Wim Leers added 2 commits

    added 2 commits

    • f443c348 - Follow-up to previous commit: generate a matching `Regex` constraint for...
    • 0b049d68 - `src/Plugin/Adapters` -> `src/Plugin/Adapter` — i.e. singular, not plural, to...

    Compare with previous version

  • Wim Leers added 1 commit

    added 1 commit

    • ebf23589 - Add one more `ksort()` I overlooked 🫣

    Compare with previous version

  • Wim Leers
  • Wim Leers added 2 commits

    added 2 commits

    • 5084a871 - Use clearer plugin IDs (that also do not match the class name) and labels that...
    • 70435889 - Update `@todo` — key parts are in place now :)

    Compare with previous version

  • Wim Leers added 3 commits

    added 3 commits

    • f30dc0b1 - Fix the image style adapter: lazy evaluation — just like `template_preprocess_image_style()`.
    • 41207cf1 - Forgot one plugin ID.
    • 7b53b8a5 - Finishing touches.

    Compare with previous version

  • Wim Leers approved this merge request

    approved this merge request

  • Wim Leers changed the description

    changed the description

  • Wim Leers requested review from @tedbow

    requested review from @tedbow

  • Ted Bowman approved this merge request

    approved this merge request

  • Feliksas Mazeikis started a merge train

    started a merge train

  • Ted Bowman
  • 883 'imageStyle' => [],
    884 ],
    885 'image_url_rel_to_abs' => [
    886 'image' => ['ℹ︎image␟{src↝entity␜ℹ︎␜entity:file␝uri␞0␟value, alt↠alt, width↠width, height↠height}'],
    887 ],
    888 ],
    889 // adapter - instance
    890 'adapter_matches_instance' => [
    891 'image_apply_style' => [
    892 'image' => ['ℹ︎␜entity:node:foo␝field_silly_image␞␟{src↝entity␜ℹ︎␜entity:file␝uri␞␟value, alt↠alt, width↠width, height↠height}'],
    893 'imageStyle' => [],
    894 ],
    895 'image_url_rel_to_abs' => [
    896 'image' => ['ℹ︎␜entity:node:foo␝field_silly_image␞␟{src↝entity␜ℹ︎␜entity:file␝uri␞␟value, alt↠alt, width↠width, height↠height}'],
    897 ],
    898 ],
  • Wim Leers mentioned in merge request !20 (merged)

    mentioned in merge request !20 (merged)

  • assigned to @larowlan

  • Please register or sign in to reply
    Loading