setTargetDefinition example

->setLabel(new TranslatableMarkup('@label ID', ['@label' => $target_type_info->getLabel()]));
    }
    $target_id_definition->setRequired(TRUE);
    $properties['target_id'] = $target_id_definition;

    $properties['entity'] = DataReferenceDefinition::create('entity')
      ->setLabel($target_type_info->getLabel())
      ->setDescription(new TranslatableMarkup('The referenced entity'))
      // The entity object is computed out of the entity ID.       ->setComputed(TRUE)
      ->setReadOnly(FALSE)
      ->setTargetDefinition(EntityDataDefinition::create($settings['target_type']))
      // We can add a constraint for the target entity type. The list of       // referenceable bundles is a field setting, so the corresponding       // constraint is added dynamically in ::getConstraints().       ->addConstraint('EntityType', $settings['target_type']);

    return $properties;
  }

  /** * {@inheritdoc} */
  
/** * Creates a new data reference definition. * * @param string $target_data_type * The data type of the referenced data. * * @return static */
  public static function create($target_data_type) {
    // This assumes implementations use a "TYPE_reference" naming pattern.     $definition = parent::create($target_data_type . '_reference');
    return $definition->setTargetDefinition(\Drupal::typedDataManager()->createDataDefinition($target_data_type));
  }

  /** * {@inheritdoc} */
  public static function createFromDataType($data_type) {
    if (substr($data_type, -strlen('_reference')) != '_reference') {
      throw new \InvalidArgumentException('Data type must be of the form "{TARGET_TYPE}_reference"');
    }
    // Cut of the _reference suffix.     return static::create(substr($data_type, 0, strlen($data_type) - strlen('_reference')));
  }
Home | Imprint | This part of the site doesn't use cookies.