getPropertyInstance example



  /** * {@inheritdoc} */
  public function __construct(ComplexDataDefinitionInterface $definition$name = NULL, TypedDataInterface $parent = NULL) {
    parent::__construct($definition$name$parent);
    // Initialize computed properties by default, such that they get cloned     // with the whole item.     foreach ($this->definition->getPropertyDefinitions() as $name => $definition) {
      if ($definition->isComputed()) {
        $this->properties[$name] = \Drupal::typedDataManager()->getPropertyInstance($this$name);
      }
    }
  }

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

  

  public function createInstance($field_type, array $configuration = []) {
    $configuration['data_definition'] = $configuration['field_definition']->getItemDefinition();
    return $this->typedDataManager->createInstance("field_item:$field_type", $configuration);
  }

  /** * {@inheritdoc} */
  public function createFieldItemList(FieldableEntityInterface $entity$field_name$values = NULL) {
    // Leverage prototyping of the Typed Data API for fast instantiation.     return $this->typedDataManager->getPropertyInstance($entity->getTypedData()$field_name$values);
  }

  /** * {@inheritdoc} */
  public function createFieldItem(FieldItemListInterface $items$index$values = NULL) {
    // Leverage prototyping of the Typed Data API for fast instantiation.     return $this->typedDataManager->getPropertyInstance($items$index$values);
  }

  /** * {@inheritdoc} */
if (!isset($type_definition)) {
      throw new \InvalidArgumentException("Invalid data type '$item_type' has been given");
    }
    $class = $type_definition['list_definition_class'];
    return $class::createFromItemType($item_type);
  }

  /** * {@inheritdoc} */
  public function getInstance(array $options) {
    return $this->getPropertyInstance($options['object']$options['property']$options['value']);
  }

  /** * {@inheritdoc} */
  public function getPropertyInstance(TypedDataInterface $object$property_name$value = NULL) {
    // For performance, try to reuse existing prototypes instead of     // constructing new objects when possible. A prototype is reused when     // creating a data object:     // - for a similar root object (same data type and settings),     // - at the same property path under that root object.
$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} */
/** * {@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);
    
Home | Imprint | This part of the site doesn't use cookies.