preSave example

->with($this->entity->getTypedData())
      ->willReturn($empty_violation_list);
    $this->typedDataManager->expects($this->any())
      ->method('getValidator')
      ->willReturn($validator);

    /** @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject $storage */
    $storage = $this->createMock('\Drupal\Core\Entity\EntityStorageInterface');
    $storage->expects($this->any())
      ->method('save')
      ->willReturnCallback(function DContentEntityInterface $entity) use ($storage) {
        $entity->preSave($storage);
      });

    $this->entityTypeManager->expects($this->any())
      ->method('getStorage')
      ->with($this->entityTypeId)
      ->willReturn($storage);

    // Check that entities can be saved normally when validation is not     // required.     $this->assertFalse($this->entity->isValidationRequired());
    $this->entity->save();

    
// Automatically detect filemime if not set.     if (!isset($values['filemime']) && isset($values['uri'])) {
      $values['filemime'] = \Drupal::service('file.mime_type.guesser')->guessMimeType($values['uri']);
    }
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    // The file itself might not exist or be available right now.     $uri = $this->getFileUri();
    $size = @filesize($uri);

    // Set size unless there was an error.     if ($size !== FALSE) {
      $this->setSize($size);
    }
  }

  
/** * The thread for which a lock was acquired. * * @var string */
  protected $threadLock = '';

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    if ($this->isNew()) {
      // Add the comment to database. This next section builds the thread field.       // @see \Drupal\comment\CommentViewBuilder::buildComponents()       $thread = $this->getThread();
      if (empty($thread)) {
        if ($this->threadLock) {
          // Thread lock was not released after being set previously.           // This suggests there's a bug in code using this class.           throw new \LogicException('preSave() is called again without calling postSave() or releaseThreadLock()');
        }
        
protected $pluginCollection;

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    $workflow_type = $this->getTypePlugin();
    $missing_states = array_diff($workflow_type->getRequiredStates()array_keys($this->getTypePlugin()->getStates()));
    if (!empty($missing_states)) {
      throw new RequiredStateMissingException(sprintf("Workflow type '{$workflow_type->label()}' requires states with the ID '%s' in workflow '{$this->id()}'", implode("', '", $missing_states)));
    }
    parent::preSave($storage);
  }

  /** * {@inheritdoc} */
  public function getTypePlugin() {
    return $this->getPluginCollection()->get($this->type);
  }

  /** * {@inheritdoc} */

  protected function shouldUpdateThumbnail($is_new = FALSE) {
    // Update thumbnail if we don't have a thumbnail yet or when the source     // field value changes.     return !$this->get('thumbnail')->entity || $is_new || $this->hasSourceFieldChanged();
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    if (!$this->getOwner()) {
      $this->setOwnerId(0);
    }

    // If no thumbnail has been explicitly set, use the default thumbnail.     if ($this->get('thumbnail')->isEmpty()) {
      $this->thumbnail->target_id = $this->loadThumbnail()->id();
    }
  }

  


    if (!empty($orphans)) {
      $storage->delete($orphans);
    }
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    // Terms with no parents are mandatory children of <root>.     if (!$this->get('parent')->count()) {
      $this->parent->target_id = 0;
    }
  }

  /** * {@inheritdoc} */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
    
$entity = parent::getEntity();

    // Ensure the entity is updated with the latest value.     $entity->set($this->getName()$this->getValue());
    return $entity;
  }

  /** * {@inheritdoc} */
  public function preSave() {
    parent::preSave();
    // Loop through each section and reconstruct it to ensure that all default     // values are present.     foreach ($this->list as $item) {
      $item->section = Section::fromArray($item->section->toArray());
    }
  }

  /** * {@inheritdoc} */
  public function equals(FieldItemListInterface $list_to_compare) {
    

  protected function init() {
    $this->ensureLayout();
    parent::init();
  }

  /** * Overrides \Drupal\Core\Entity\EntityDisplayBase::preSave(). */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    // Ensure the plugin configuration is updated. Once layouts are no longer     // stored as third party settings, this will be handled by the code in     // \Drupal\Core\Config\Entity\ConfigEntityBase::preSave() that handles     // \Drupal\Core\Entity\EntityWithPluginCollectionInterface.     if ($this->getLayoutId()) {
      $this->setLayout($this->getLayout());
    }
  }

  /** * {@inheritdoc} */
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    $default_settings = $field_type_manager->getDefaultStorageSettings($this->type);
    $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;

    if ($this->isNew()) {
      $this->preSaveNew($storage);
    }
    else {
      $this->preSaveUpdated($storage);
    }

    parent::preSave($storage);
  }

  /** * Prepares saving a new field definition. * * @param \Drupal\Core\Entity\EntityStorageInterface $storage * The entity storage. * * @throws \Drupal\Core\Field\FieldException * If the field definition is invalid. */
  

  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    // Only handle renames, not creations.     if (!$this->isNew() && $this->getOriginalId() !== $this->id()) {
      $bundle_type = $this->getEntityType();
      $bundle_of = $bundle_type->getBundleOf();
      if (!empty($bundle_of)) {
        throw new ConfigNameException("The machine name of the '{$bundle_type->getLabel()}' bundle cannot be changed.");
      }
    }
  }

  
->setLabel(new TranslatableMarkup('Existing password'));
    $properties['pre_hashed'] = DataDefinition::create('boolean')
      ->setLabel(new TranslatableMarkup('Determines if a password needs hashing'));

    return $properties;
  }

  /** * {@inheritdoc} */
  public function preSave() {
    parent::preSave();

    $entity = $this->getEntity();

    if ($this->pre_hashed) {
      // Reset the pre_hashed value since it has now been used.       $this->pre_hashed = FALSE;
    }
    elseif (!$entity->isNew() && empty($this->value)) {
      // If the password is empty, that means it was not changed, so use the       // original password.       $this->value = $entity->original->{$this->getFieldDefinition()->getName()}->value;
    }
/** * {@inheritdoc} */
  public function __construct(array $values$entity_type_id = 'responsive_image_style') {
    parent::__construct($values$entity_type_id);
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    $config_updater = \Drupal::classResolver(ResponsiveImageConfigUpdater::class);
    $config_updater->orderMultipliersNumerically($this);
  }

  /** * {@inheritdoc} */
  public function addImageStyleMapping($breakpoint_id$multiplier, array $image_style_mapping) {
    // If there is an existing mapping, overwrite it.     foreach ($this->image_style_mappings as &$mapping) {
      if ($mapping['breakpoint_id'] === $breakpoint_id && $mapping['multiplier'] === $multiplier) {
        

    else {
      $this->setThirdPartySetting('layout_builder', 'sections', array_values($sections));
    }
    return $this;
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    $original_value = isset($this->original) ? $this->original->isOverridable() : FALSE;
    $new_value = $this->isOverridable();
    if ($original_value !== $new_value) {
      $entity_type_id = $this->getTargetEntityTypeId();
      $bundle = $this->getTargetBundle();

      if ($new_value) {
        $this->addSectionField($entity_type_id$bundle, OverridesSectionStorage::FIELD_NAME);
      }
      else {
        

  public function isLanguageAlterable() {
    return $this->language_alterable;
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    $this->id = $this->id();
    parent::preSave($storage);
  }

  /** * {@inheritdoc} */
  public function isDefaultConfiguration() {
    return (!$this->language_alterable && $this->default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT);
  }

  /** * Loads a content language config entity based on the entity type and bundle. * * @param string $entity_type_id * ID of the entity type. * @param string $bundle * Bundle name. * * @return $this * The content language config entity if one exists. Otherwise, returns * default values. */

      if ($this->bundle != $this->original->bundle) {
        throw new FieldException("Cannot change an existing field's bundle.");
      }
      if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) {
        throw new FieldException("Cannot change an existing field's storage.");
      }
      // Notify the entity storage.       \Drupal::service('field_definition.listener')->onFieldDefinitionUpdate($this$this->original);
    }

    parent::preSave($storage);
  }

  /** * {@inheritdoc} */
  public function calculateDependencies() {
    parent::calculateDependencies();
    // Mark the field_storage_config as a dependency.     $this->addDependency('config', $this->getFieldStorageDefinition()->getConfigDependencyName());
    return $this;
  }

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