hasNewEntity example


  }

  /** * {@inheritdoc} * * This has to be overridden because FileFormatterBase expects $item to be * of type \Drupal\file\Plugin\Field\FieldType\FileItem and calls * isDisplayed() which is not in FieldItemInterface. */
  protected function needsEntityLoad(EntityReferenceItem $item) {
    return !$item->hasNewEntity();
  }

  /** * {@inheritdoc} */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form$form_state);
    $link_types = [
      'content' => $this->t('Content'),
      'media' => $this->t('Media item'),
    ];
    


  /** * {@inheritdoc} */
  public function getValue() {
    $values = parent::getValue();

    // If there is an unsaved entity, return it as part of the field item values     // to ensure idempotency of getValue() / setValue().     if ($this->hasNewEntity()) {
      $values['entity'] = $this->entity;
    }
    return $values;
  }

  /** * {@inheritdoc} */
  public function onChange($property_name$notify = TRUE) {
    // Make sure that the target ID and the target property stay in sync.     if ($property_name == 'entity') {
      
if ($this->isEmpty()) {
      return [];
    }

    // Collect the IDs of existing entities to load, and directly grab the     // "autocreate" entities that are already populated in $item->entity.     $target_entities = $ids = [];
    foreach ($this->list as $delta => $item) {
      if ($item->target_id !== NULL) {
        $ids[$delta] = $item->target_id;
      }
      elseif ($item->hasNewEntity()) {
        $target_entities[$delta] = $item->entity;
      }
    }

    // Load and add the existing entities.     if ($ids) {
      $target_type = $this->getFieldDefinition()->getSetting('target_type');
      $entities = \Drupal::entityTypeManager()->getStorage($target_type)->loadMultiple($ids);
      foreach ($ids as $delta => $target_id) {
        if (isset($entities[$target_id])) {
          $target_entities[$delta] = $entities[$target_id];
        }
$target_entities = \Drupal::entityTypeManager()->getStorage($target_type)->loadMultiple($ids);
    }

    // For each item, pre-populate the loaded entity in $item->entity, and set     // the 'loaded' flag.     foreach ($entities_items as $items) {
      foreach ($items as $item) {
        if (isset($target_entities[$item->target_id])) {
          $item->entity = $target_entities[$item->target_id];
          $item->_loaded = TRUE;
        }
        elseif ($item->hasNewEntity()) {
          $item->_loaded = TRUE;
        }
      }
    }
  }

  /** * Returns whether the entity referenced by an item needs to be loaded. * * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item * The item to check. * * @return bool * TRUE if the entity needs to be loaded. */

  public function validate($value, Constraint $constraint) {
    // The validator should run only if we are in a active workspace context.     if (!$this->workspaceManager->hasActiveWorkspace()) {
      return;
    }

    $target_entity_type_id = $value->getFieldDefinition()->getFieldStorageDefinition()->getSetting('target_type');
    $target_entity_type = $this->entityTypeManager->getDefinition($target_entity_type_id);

    if ($value->hasNewEntity() && !$this->workspaceManager->isEntityTypeSupported($target_entity_type)) {
      $this->context->addViolation($constraint->message, ['%collection_label' => $target_entity_type->getCollectionLabel()]);
    }
  }

}
Home | Imprint | This part of the site doesn't use cookies.