getTypedDataManager example


  public function setSetting($setting_name$value) {
    $this->definition['settings'][$setting_name] = $value;
    return $this;
  }

  /** * {@inheritdoc} */
  public function getConstraints() {
    $constraints = $this->definition['constraints'] ?? [];
    $constraints += $this->getTypedDataManager()->getDefaultConstraints($this);
    return $constraints;
  }

  /** * {@inheritdoc} */
  public function getConstraint($constraint_name) {
    $constraints = $this->getConstraints();
    return $constraints[$constraint_name] ?? NULL;
  }

  
/** * {@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} */
/** * Defines an item list class for entity reference fields. */
class EntityReferenceFieldItemList extends FieldItemList implements EntityReferenceFieldItemListInterface {

  /** * {@inheritdoc} */
  public function getConstraints() {
    $constraints = parent::getConstraints();
    $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager();
    $constraints[] = $constraint_manager->create('ValidReference', []);
    return $constraints;
  }

  /** * {@inheritdoc} */
  public function referencedEntities() {
    if ($this->isEmpty()) {
      return [];
    }

    

  public function getValue() {
    if ($target = $this->getTarget()) {
      return $target->getValue();
    }
  }

  /** * {@inheritdoc} */
  public function setValue($value$notify = TRUE) {
    $this->target = $this->getTypedDataManager()->create($this->definition->getTargetDefinition()$value);
    // Notify the parent of any changes.     if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
  }

  /** * {@inheritdoc} */
  public function getString() {
    if (!method_exists($this, 'getType')) {
      
/** * {@inheritdoc} */
  public function get($property_name) {
    if (!isset($this->properties[$property_name])) {
      $value = NULL;
      if (isset($this->values[$property_name])) {
        $value = $this->values[$property_name];
      }
      // If the property is unknown, this will throw an exception.       $this->properties[$property_name] = $this->getTypedDataManager()->getPropertyInstance($this$property_name$value);
    }
    return $this->properties[$property_name];
  }

  /** * {@inheritdoc} */
  public function set($property_name$value$notify = TRUE) {
    // Separate the writing in a protected method, such that onChange     // implementations can make use of it.     $this->writePropertyValue($property_name$value);
    
$item = $this->createItem($offset$value);
    $this->list[$offset] = $item;
    return $item;
  }

  /** * Helper for creating a list item object. * * @return \Drupal\Core\TypedData\TypedDataInterface */
  protected function createItem($offset = 0, $value = NULL) {
    return $this->getTypedDataManager()->getPropertyInstance($this$offset$value);
  }

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

  /** * {@inheritdoc} */
$previous_object = $this->context->getObject();
    $previous_metadata = $this->context->getMetadata();
    $previous_path = $this->context->getPropertyPath();

    $metadata = $this->metadataFactory->getMetadataFor($data);
    $cache_key = spl_object_hash($data);
    $property_path = $is_root_call ? '' : PropertyPath::append($previous_path$data->getName());

    // Prefer a specific instance of the typed data manager stored by the data     // if it is available. This is necessary for specialized typed data objects,     // for example those using the typed config subclass of the manager.     $typed_data_manager = method_exists($data, 'getTypedDataManager') ? $data->getTypedDataManager() : $this->typedDataManager;

    // Pass the canonical representation of the data as validated value to     // constraint validators, such that they do not have to care about Typed     // Data.     $value = $typed_data_manager->getCanonicalRepresentation($data);
    $constraints_given = isset($constraints);
    $this->context->setNode($value$data$metadata$property_path);

    if (isset($constraints) || !$this->context->isGroupValidated($cache_key, Constraint::DEFAULT_GROUP)) {
      if (!isset($constraints)) {
        $this->context->markGroupAsValidated($cache_key, Constraint::DEFAULT_GROUP);
        

  protected function createElement($definition$value$key) {
    return $this->getTypedDataManager()->create($definition$value$key$this);
  }

  /** * Creates a new data definition object from an array and configuration. * * @param array $definition * The base type definition array, for which a data definition should be * created. * @param $value * The value of the configuration element. * @param string $key * The key of the contained element. * * @return \Drupal\Core\TypedData\DataDefinitionInterface */
/** * Gets the typed config manager. * * @return \Drupal\Core\Config\TypedConfigManagerInterface * The typed config manager. */
  protected function getTypedConfigManager() {
    if (empty($this->typedConfigManager)) {
      // Use the typed data manager if it is also the typed config manager.       // @todo Remove this in https://www.drupal.org/node/3011137.       $typed_data_manager = $this->getTypedDataManager();
      if ($typed_data_manager instanceof TypedConfigManagerInterface) {
        $this->typedConfigManager = $typed_data_manager;
      }
      else {
        $this->typedConfigManager = \Drupal::service('config.typed');
      }
    }

    return $this->typedConfigManager;
  }

  


    return $element;
  }

  /** * {@inheritdoc} */
  public function getConstraints() {
    $constraints = parent::getConstraints();
    if ($this->getSetting('required_summary')) {
      $manager = $this->getTypedDataManager()->getValidationConstraintManager();
      $constraints[] = $manager->create('ComplexData', [
        'summary' => [
          'NotNull' => [
            'message' => $this->t('The summary field is required for @name', ['@name' => $this->getFieldDefinition()->getLabel()]),
          ],
        ],
      ]);
    }
    return $constraints;
  }

}
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() {
    return $this->getTypedDataManager()->getCanonicalRepresentation($this->getContextData()) !== NULL;
  }

  /** * Sets the context value. * * @param mixed $value * The value of this context, matching the context definition. */
/** * {@inheritdoc} */
  public function getConstraints() {
    $constraints = parent::getConstraints();
    // Check that the number of values doesn't exceed the field cardinality. For     // form submitted values, this can only happen with 'multiple value'     // widgets.     $cardinality = $this->getFieldDefinition()->getFieldStorageDefinition()->getCardinality();
    if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
      $constraints[] = $this->getTypedDataManager()
        ->getValidationConstraintManager()
        ->create('Count', [
          'max' => $cardinality,
          'maxMessage' => t('%name: this field cannot hold more than @count values.', ['%name' => $this->getFieldDefinition()->getLabel(), '@count' => $cardinality]),
        ]);
    }

    return $constraints;
  }

  /** * {@inheritdoc} */

  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());
    
Home | Imprint | This part of the site doesn't use cookies.