getDataType example


  public function addConstraint($constraint_name$options = NULL) {
    $this->constraints[$constraint_name] = $options;
    return $this;
  }

  /** * {@inheritdoc} */
  public function getDataDefinition() {
    if ($this->isMultiple()) {
      $definition = $this->getTypedDataManager()->createListDataDefinition($this->getDataType());
    }
    else {
      $definition = $this->getTypedDataManager()->createDataDefinition($this->getDataType());
    }

    if (!$definition) {
      throw new \Exception("The data type '{$this->getDataType()}' is invalid");
    }
    $definition->setLabel($this->getLabel())
      ->setDescription($this->getDescription())
      ->setRequired($this->isRequired());
    
try {
      $target_type_info = \Drupal::entityTypeManager()->getDefinition($target_type);
    }
    catch (PluginNotFoundException $e) {
      throw new FieldException(sprintf("Field '%s' on entity type '%s' references a target entity type '%s' which does not exist.",
        $field_definition->getName(),
        $field_definition->getTargetEntityTypeId(),
        $target_type
      ));
    }
    $properties = static::propertyDefinitions($field_definition)['target_id'];
    if ($target_type_info->entityClassImplements(FieldableEntityInterface::class) && $properties->getDataType() === 'integer') {
      $columns = [
        'target_id' => [
          'description' => 'The ID of the target entity.',
          'type' => 'int',
          'unsigned' => TRUE,
        ],
      ];
    }
    else {
      $columns = [
        'target_id' => [
          
// Test getting metadata upfront. The entity types used for this test have     // a default bundle that is the same as the entity type.     $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type$entity_type);
    $this->assertEquals('string', $definitions['name']->getType()$entity_type . ': Name field found.');
    $this->assertEquals('entity_reference', $definitions['user_id']->getType()$entity_type . ': User field found.');
    $this->assertEquals('text', $definitions['field_test_text']->getType()$entity_type . ': Test-text-field field found.');

    // Test deriving further metadata.     $this->assertInstanceOf(FieldDefinitionInterface::class$definitions['name']);
    $field_item_definition = $definitions['name']->getItemDefinition();
    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$field_item_definition);
    $this->assertEquals('field_item:string', $field_item_definition->getDataType());
    $value_definition = $field_item_definition->getPropertyDefinition('value');
    $this->assertInstanceOf(DataDefinitionInterface::class$value_definition);
    $this->assertEquals('string', $value_definition->getDataType());

    // Test deriving metadata from references.     $entity_definition = EntityDataDefinition::create($entity_type);
    $langcode_key = $this->entityTypeManager->getDefinition($entity_type)->getKey('langcode');
    $reference_definition = $entity_definition->getPropertyDefinition($langcode_key)
      ->getPropertyDefinition('language')
      ->getTargetDefinition();
    $this->assertEquals('language', $reference_definition->getDataType());

    
public function getContextValue() {
    if (!isset($this->contextData)) {
      $definition = $this->getContextDefinition();
      $default_value = $definition->getDefaultValue();

      if (isset($default_value)) {
        // Keep the default value here so that subsequent calls don't have to         // look it up again.         $this->setContextValue($default_value);
      }
      elseif ($definition->isRequired()) {
        $type = $definition->getDataType();
        throw new ContextException("The '$type' context is required and not present.");
      }
      return $default_value;
    }
    return $this->getTypedDataManager()->getCanonicalRepresentation($this->contextData);
  }

  /** * {@inheritdoc} */
  public function hasContextValue() {
    
// Test an authenticated account.     $authenticated = User::create([
      'name' => $this->randomMachineName(),
    ]);
    $authenticated->save();
    $authenticated = User::load($authenticated->id());
    $this->container->get('current_user')->setAccount($authenticated);

    $contexts = $context_repository->getAvailableContexts();
    $this->assertArrayHasKey('@user.current_user_context:current_user', $contexts);
    $this->assertSame('entity:user', $contexts['@user.current_user_context:current_user']->getContextDefinition()->getDataType());
    $this->assertTrue($contexts['@user.current_user_context:current_user']->hasContextValue());
    $this->assertNotNull($contexts['@user.current_user_context:current_user']->getContextValue());

    // Test an anonymous account.     $anonymous = $this->prophesize(AccountInterface::class);
    $anonymous->id()->willReturn(0);
    $this->container->get('current_user')->setAccount($anonymous->reveal());

    $contexts = $context_repository->getAvailableContexts();
    $this->assertArrayHasKey('@user.current_user_context:current_user', $contexts);
    $this->assertSame('entity:user', $contexts['@user.current_user_context:current_user']->getContextDefinition()->getDataType());
    
/** * @covers ::getAvailableContexts */
  public function testGetAvailableContexts() {
    $context_repository = $this->container->get('context.repository');

    // Test taxonomy_term.taxonomy_term_route_context:taxonomy_term exists.     $contexts = $context_repository->getAvailableContexts();
    $this->assertArrayHasKey('@taxonomy_term.taxonomy_term_route_context:taxonomy_term', $contexts);
    $this->assertSame('entity:taxonomy_term', $contexts['@taxonomy_term.taxonomy_term_route_context:taxonomy_term']->getContextDefinition()
      ->getDataType());
  }

  /** * @covers ::getRuntimeContexts */
  public function testGetRuntimeContexts() {
    // Create term.     $vocabulary = $this->createVocabulary();
    $term = $this->createTerm($vocabulary);

    // Create RouteMatch from term entity.
public function testFields() {
    $field_definition = BaseFieldDefinition::create('integer');
    // Fields are lists of complex data.     $this->assertInstanceOf(ListDataDefinitionInterface::class$field_definition);
    $this->assertNotInstanceOf(ComplexDataDefinitionInterface::class$field_definition);
    $field_item_definition = $field_definition->getItemDefinition();
    $this->assertNotInstanceOf(ListDataDefinitionInterface::class$field_item_definition);
    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$field_item_definition);

    // Derive metadata about field item properties.     $this->assertEquals(['value']array_keys($field_item_definition->getPropertyDefinitions()));
    $this->assertEquals('integer', $field_item_definition->getPropertyDefinition('value')->getDataType());
    $this->assertEquals('value', $field_item_definition->getMainPropertyName());
    $this->assertNull($field_item_definition->getPropertyDefinition('invalid'));

    // Test accessing field item property metadata via the field definition.     $this->assertInstanceOf(FieldDefinitionInterface::class$field_definition);
    $this->assertEquals(['value']array_keys($field_definition->getPropertyDefinitions()));
    $this->assertEquals('integer', $field_definition->getPropertyDefinition('value')->getDataType());
    $this->assertEquals('value', $field_definition->getMainPropertyName());
    $this->assertNull($field_definition->getPropertyDefinition('invalid'));

    // Test using the definition factory for field item lists and field items.
throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $data_type));
    }
    $typed_data = $class::createInstance($data_definition$configuration['name']$configuration['parent']);
    $typed_data->setTypedDataManager($this);
    return $typed_data;
  }

  /** * {@inheritdoc} */
  public function create(DataDefinitionInterface $definition$value = NULL, $name = NULL, $parent = NULL) {
    $typed_data = $this->createInstance($definition->getDataType()[
      'data_definition' => $definition,
      'name' => $name,
      'parent' => $parent,
    ]);
    if (isset($value)) {
      $typed_data->setValue($value, FALSE);
    }
    return $typed_data;
  }

  /** * {@inheritdoc} */
$this->drupalLogin($this->drupalCreateUser([
      'administer views',
      'administer blocks',
    ]));

    // Check if context was correctly propagated to the block.     $definition = $this->container->get('plugin.manager.block')
      ->getDefinition('views_block:test_view_block_with_context-block_1');
    $this->assertInstanceOf(ContextDefinitionInterface::class$definition['context_definitions']['nid']);
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
    $context = $definition['context_definitions']['nid'];
    $this->assertEquals('entity:node', $context->getDataType(), 'Context definition data type is correct.');
    $this->assertEquals('Content: ID', $context->getLabel(), 'Context definition label is correct.');
    $this->assertFalse($context->isRequired(), 'Context is not required.');

    // Place test block via block UI to check if contexts are correctly exposed.     $this->drupalGet(
      'admin/structure/block/add/views_block:test_view_block_with_context-block_1/stark',
      ['query' => ['region' => 'content']]
    );
    $edit = [
      'settings[context_mapping][nid]' => '@node.node_route_context:node',
    ];
    
/** * {@inheritdoc} */
  public function getPluginId() {
    return $this->definition['type'];
  }

  /** * {@inheritdoc} */
  public function getPluginDefinition() {
    return $this->getTypedDataManager()->getDefinition($this->definition->getDataType());
  }

  /** * {@inheritdoc} */
  public function getDataDefinition() {
    return $this->definition;
  }

  /** * {@inheritdoc} */
// Test iterating.     $count = 0;
    foreach ($typed_data as $item) {
      $this->assertInstanceOf(TypedDataInterface::class$item);
      $count++;
    }
    $this->assertEquals(3, $count);

    // Test retrieving metadata.     $this->assertEquals(array_keys($value)array_keys($typed_data->getDataDefinition()->getPropertyDefinitions()));
    $definition = $typed_data->getDataDefinition()->getPropertyDefinition('one');
    $this->assertEquals('string', $definition->getDataType());
    $this->assertNull($typed_data->getDataDefinition()->getPropertyDefinition('invalid'));

    // Test getting and setting properties.     $this->assertEquals('eins', $typed_data->get('one')->getValue());
    $this->assertEquals($value$typed_data->toArray());
    $typed_data->set('one', 'uno');
    $this->assertEquals('uno', $typed_data->get('one')->getValue());
    // Make sure the update is reflected in the value of the map also.     $value = $typed_data->getValue();
    $this->assertEquals(['one' => 'uno', 'two' => 'zwei', 'three' => 'drei']$value);

    


    private function fetchMessage(): bool
    {
        if (!$this->currentResponse->resolve($this->configuration['poll_timeout'])) {
            return false;
        }

        foreach ($this->currentResponse->getMessages() as $message) {
            $headers = [];
            $attributes = $message->getMessageAttributes();
            if (isset($attributes[self::MESSAGE_ATTRIBUTE_NAME]) && 'String' === $attributes[self::MESSAGE_ATTRIBUTE_NAME]->getDataType()) {
                $headers = json_decode($attributes[self::MESSAGE_ATTRIBUTE_NAME]->getStringValue(), true);
                unset($attributes[self::MESSAGE_ATTRIBUTE_NAME]);
            }
            foreach ($attributes as $name => $attribute) {
                if ('String' !== $attribute->getDataType()) {
                    continue;
                }

                $headers[$name] = $attribute->getStringValue();
            }

            

        // If no more parts left, this is the final property.         return (string) $value;
      }
      else {
        // Get nested value and continue processing.         if ($name == '%parent') {
          /** @var \Drupal\Core\Config\Schema\ArrayElement $parent */
          // Switch replacement values with values from the parent.           $parent = $data['%parent'];
          $data = $parent->getValue();
          $data['%type'] = $parent->getDataDefinition()->getDataType();
          // The special %parent and %key values now need to point one level up.           if ($new_parent = $parent->getParent()) {
            $data['%parent'] = $new_parent;
            $data['%key'] = $new_parent->getName();
          }
        }
        else {
          $data = $data[$name];
        }
      }
    }
  }
trait TextEditorObjectDependentValidatorTrait {

  /** * Creates a text editor object from the execution context. * * Works both for an individual text editor config entity and a pair. * * @return \Drupal\editor\EditorInterface * A text editor object, with the text format pre-populated. */
  private function createTextEditorObjectFromContext(): EditorInterface {
    if ($this->context->getRoot()->getDataDefinition()->getDataType() === 'ckeditor5_valid_pair__format_and_editor') {
      $text_format = FilterFormat::create([
        'filters' => $this->context->getRoot()->get('filters')->toArray(),
      ]);
    }
    else {
      assert($this->context->getRoot()->getDataDefinition()->getDataType() === 'editor.editor.*');
      $text_format = FilterFormat::load($this->context->getRoot()->get('format')->getValue());
    }
    assert($text_format instanceof FilterFormatInterface);

    $text_editor = Editor::create([
      
return $this;
  }

  /** * {@inheritdoc} */
  public function getClass() {
    if (isset($this->definition['class'])) {
      return $this->definition['class'];
    }
    else {
      $type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
      return $type_definition['class'];
    }
  }

  /** * Sets the class used for creating the typed data object. * * @param string|null $class * The class to use. * * @return static * The object itself for chaining. */
Home | Imprint | This part of the site doesn't use cookies.