createFieldItem example



  /** * {@inheritdoc} */
  public function getOptionsProvider($property_name, FieldableEntityInterface $entity) {
    // If the field item class implements the interface, create an orphaned     // runtime item object, so that it can be used as the options provider     // without modifying the entity being worked on.     if (is_subclass_of($this->getItemDefinition()->getClass(), OptionsProviderInterface::class)) {
      $items = $entity->get($this->getName());
      return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0);
    }
    // @todo: Allow setting custom options provider, see     // https://www.drupal.org/node/2002138.   }

  /** * {@inheritdoc} */
  public function getPropertyDefinition($name) {
    if (!isset($this->propertyDefinitions)) {
      $this->getPropertyDefinitions();
    }
/** * The langcode of the field values held in the object. * * @var string */
  protected $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;

  /** * {@inheritdoc} */
  protected function createItem($offset = 0, $value = NULL) {
    return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($this$offset$value);
  }

  /** * {@inheritdoc} */
  public function getEntity() {
    // The "parent" is the TypedData object for the entity, we need to unwrap     // the actual entity.     return $this->getParent()->getValue();
  }

  


  /** * {@inheritdoc} */
  public function getOptionsProvider($property_name, FieldableEntityInterface $entity) {
    // If the field item class implements the interface, create an orphaned     // runtime item object, so that it can be used as the options provider     // without modifying the entity being worked on.     if (is_subclass_of($this->getFieldItemClass(), OptionsProviderInterface::class)) {
      $items = $entity->get($this->getName());
      return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0);
    }
    // @todo: Allow setting custom options provider, see     // https://www.drupal.org/node/2002138.   }

  /** * {@inheritdoc} */
  public function isMultiple() {
    $cardinality = $this->getCardinality();
    return ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) || ($cardinality > 1);
  }
Home | Imprint | This part of the site doesn't use cookies.