Skip to content
Snippets Groups Projects
Select Git revision
  • 974defe3fbfecc4835bceec75920d5fe43797b9d
  • 2.0.x default
  • 8.x-1.x
  • 3519543-bug--sample-entity
  • 2.0.5
  • 8.x-1.13
  • 8.x-1.12
  • 8.x-1.11
  • previous/3525646-2.1.0-copy-variant/2025-05-27
  • previous/3525775-computed-entity-ref/2025-05-27
  • 2.0.4
  • previous/3525646-2.1.0-copy-variant/2025-05-21
  • previous/3525646-2.1.0-copy-variant/2025-05-20
  • 2.0.3
  • 2.0.2
  • 2.0.1
  • 2.0.0
  • 2.0.0-rc2
  • 2.0.0-rc1
  • 2.0.0-beta6
  • 2.0.0-beta5
  • 2.0.0-beta4
  • 8.x-1.10
  • 2.0.0-beta3
24 results

Source.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Source.php 1.08 KiB
    <?php
    
    namespace Drupal\ui_patterns_field\Plugin\DataType;
    
    use Drupal\Core\StringTranslation\TranslatableMarkup;
    use Drupal\Core\TypedData\Attribute\DataType;
    use Drupal\Core\TypedData\TypedData;
    
    /**
     * Provides a data type wrapping UI Patterns source.
     *
     * @internal
     *   Plugin classes are internal.
     */
    #[DataType(
      id: "ui_patterns_source",
      label: new TranslatableMarkup("Source (UI Patterns)"),
      description: new TranslatableMarkup("Source datatype for UI Patterns"),
    )]
    class Source extends TypedData {
    
      /**
       * The pattern configuration.
       */
      protected ?string $value;
    
      /**
       * {@inheritdoc}
       */
      public function setValue($value, $notify = TRUE): void {
        if (is_array($value)) {
          $value = json_encode($value);
        }
        parent::setValue($value, $notify);
      }
    
      /**
       * {@inheritdoc}
       */
      public function getValue() {
        try {
          $value = isset($this->value) ? parent::getValue() : NULL;
          return empty($value) ? [] : json_decode($value, TRUE, 512, JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY);
        }
        catch (\Exception $e) {
          return NULL;
        }
      }
    
    }