createDuplicate example


  abstract protected function createEntity();

  /** * Creates another entity to be tested. * * @return \Drupal\Core\Entity\EntityInterface * Another entity based on $this->entity. */
  protected function createAnotherEntity() {
    $entity = $this->entity->createDuplicate();
    $label_key = $entity->getEntityType()->getKey('label');
    if ($label_key) {
      $entity->set($label_key$entity->label() . '_dupe');
    }
    $entity->save();
    return $entity;
  }

  /** * Returns the expected normalization of the entity. * * @see ::createEntity() * * @return array */
$new_id = $view->addDisplay('page', 'Test', 'test');
    $display = $view->get('display');

    // Ensure the right display_plugin is created/instantiated.     $this->assertEquals('page', $display[$new_id]['display_plugin'], 'New page display "test" uses the right display plugin.');

    $executable = $view->getExecutable();
    $executable->initDisplay();
    $this->assertInstanceOf(Page::class$executable->displayHandlers->get($new_id));

    // To save this with a new ID, we should use createDuplicate().     $view = $view->createDuplicate();
    $view->set('id', 'test_view_storage_new_new2');
    $view->save();
    $values = $this->config('views.view.test_view_storage_new_new2')->get();

    // Verify that the display was saved by ensuring it contains an array of     // values in the view data.     $this->assertIsArray($values['display']['test']);
  }

  /** * Tests the display related functions like getDisplaysList(). */
->getStorage($entity_type);
    $storage->resetCache([$entity->id()]);
    $entity_loaded = $storage->load($entity->id());
    $this->assertSame($uuid$entity_loaded->uuid());

    // Verify that \Drupal::service('entity.repository')->loadEntityByUuid() loads the same entity.     $entity_loaded_by_uuid = \Drupal::service('entity.repository')->loadEntityByUuid($entity_type$uuid, TRUE);
    $this->assertSame($uuid$entity_loaded_by_uuid->uuid());
    $this->assertEquals($entity_loaded->id()$entity_loaded_by_uuid->id());

    // Creating a duplicate needs to result in a new UUID.     $entity_duplicate = $entity->createDuplicate();
    foreach ($entity->getFields() as $property => $value) {
      switch ($property) {
        case 'uuid':
          $this->assertNotNull($entity_duplicate->uuid());
          $this->assertNotNull($entity->uuid());
          $this->assertNotEquals($entity->uuid()$entity_duplicate->uuid());
          break;

        case 'id':
          $this->assertNull($entity_duplicate->id());
          $this->assertNotNull($entity->id());
          


  /** * Form submission handler for the 'clone' action. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * A reference to a keyed array containing the current state of the form. */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity = $this->entity->createDuplicate();
    $this->entity->set('label', $form_state->getValue('label'));
    $this->entity->set('id', $form_state->getValue('id'));
    $this->entity->save();

    // Redirect the user to the view admin form.     $form_state->setRedirectUrl($this->entity->toUrl('edit-form'));
  }

}
    // id because a new revision wasn't created, the existing revision was     // updated.     $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
    $this->assertEquals($loaded->getRevisionId()$loadedRevisionId);
    $this->assertEquals($loaded->getRevisionId()$loaded->getLoadedRevisionId());

    // Creating a clone should keep the loaded Revision ID.     $clone = clone $loaded;
    $this->assertSame($loaded->getLoadedRevisionId()$clone->getLoadedRevisionId());

    // Creating a duplicate should set a NULL loaded Revision ID.     $duplicate = $loaded->createDuplicate();
    $this->assertNull($duplicate->getLoadedRevisionId());
  }

  /** * Tests the loaded revision ID for translatable entities. */
  public function testTranslatedLoadedRevisionId() {
    ConfigurableLanguage::createFromLangcode('fr')->save();

    // Create a basic EntityTestMulRev entity and save it.     $entity = EntityTestMulRev::create();
    
/** * {@inheritdoc} */
  public function isUninstalling() {
    return $this->isUninstalling;
  }

  /** * {@inheritdoc} */
  public function createDuplicate() {
    $duplicate = parent::createDuplicate();

    // Prevent the new duplicate from being misinterpreted as a rename.     $duplicate->setOriginalId(NULL);
    return $duplicate;
  }

  /** * Helper callback for uasort() to sort configuration entities by weight and label. */
  public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
    $a_weight = $a->weight ?? 0;
    
// Check that our call to action message is appealing to filibusters.     $this->assertEquals('Draw yer cutlasses!', $block->label());

    // Check that the cacheability metadata is correct.     $this->assertEquals(['pirate_day']$block->getCacheContexts());
    $this->assertEquals(['config:block.block.call_to_action', 'pirate-day-tag']$block->getCacheTags());
    $this->assertEquals(PirateDayCacheContext::PIRATE_DAY_MAX_AGE, $block->getCacheMaxAge());

    // Check that duplicating a config entity does not have the original config     // entity's cache tag.     $this->assertEquals(['config:block.block.', 'pirate-day-tag']$block->createDuplicate()->getCacheTags());

    // Check that renaming a config entity does not have the original config     // entity's cache tag.     $block->set('id', 'call_to_looting')->save();
    $this->assertEquals(['pirate_day']$block->getCacheContexts());
    $this->assertEquals(['config:block.block.call_to_looting', 'pirate-day-tag']$block->getCacheTags());
    $this->assertEquals(PirateDayCacheContext::PIRATE_DAY_MAX_AGE, $block->getCacheMaxAge());
  }

}
unset($properties['hidden'][$field_name]);
      }
    }

    return $properties;
  }

  /** * {@inheritdoc} */
  public function createCopy($mode) {
    $display = $this->createDuplicate();
    $display->mode = $display->originalMode = $mode;
    return $display;
  }

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

  

  public function form(array $form, FormStateInterface $form_state) {
    if ($this->operation == 'duplicate') {
      $form['#title'] = $this->t('<em>Duplicate responsive image style</em> @label', ['@label' => $this->entity->label()]);
      $this->entity = $this->entity->createDuplicate();
    }
    if ($this->operation == 'edit') {
      $form['#title'] = $this->t('<em>Edit responsive image style</em> @label', ['@label' => $this->entity->label()]);
    }

    /** @var \Drupal\responsive_image\ResponsiveImageStyleInterface $responsive_image_style */
    $responsive_image_style = $this->entity;
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#maxlength' => 255,
      
'id' => 'layla',
      'label' => 'Layla',
    ]);
    $workspace->save();
    return $workspace;
  }

  /** * {@inheritdoc} */
  protected function createAnotherEntity() {
    $workspace = $this->entity->createDuplicate();
    $workspace->id = 'layla_dupe';
    $workspace->label = 'Layla_dupe';
    $workspace->save();
    return $workspace;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    $author = User::load($this->entity->getOwnerId());
    
public function saveBlockContent($new_revision = FALSE, $duplicate_block = FALSE) {
    /** @var \Drupal\block_content\BlockContentInterface $block */
    $block = NULL;
    if (!empty($this->configuration['block_serialized'])) {
      $block = unserialize($this->configuration['block_serialized']);
    }
    if ($duplicate_block) {
      if (empty($block) && !empty($this->configuration['block_revision_id'])) {
        $block = $this->entityTypeManager->getStorage('block_content')->loadRevision($this->configuration['block_revision_id']);
      }
      if ($block) {
        $block = $block->createDuplicate();
      }
    }

    if ($block) {
      // Since the content block is only set if it was unserialized, the flag       // will only effect blocks which were modified or serialized originally.       if ($new_revision) {
        $block->setNewRevision();
      }
      $block->save();
      $this->configuration['block_revision_id'] = $block->getRevisionId();
      
->activate()
      ->save();

    return $user;
  }

  /** * {@inheritdoc} */
  protected function createAnotherEntity() {
    /** @var \Drupal\user\UserInterface $user */
    $user = $this->entity->createDuplicate();
    $user->setUsername($user->label() . '_dupe');
    $user->save();
    return $user;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    return [
      'uid' => [
        [
$this->entityType->expects($this->once())
      ->method('hasKey')
      ->with('uuid')
      ->willReturn(TRUE);

    $new_uuid = '8607ef21-42bc-4913-978f-8c06207b0395';
    $this->uuid->expects($this->once())
      ->method('generate')
      ->willReturn($new_uuid);

    $duplicate = $this->entity->createDuplicate();
    $this->assertInstanceOf('\Drupal\Core\Entity\EntityBase', $duplicate);
    $this->assertNotSame($this->entity, $duplicate);
    $this->assertFalse($this->entity->isNew());
    $this->assertTrue($duplicate->isNew());
    $this->assertNull($duplicate->id());
    $this->assertNull($duplicate->getOriginalId());
    $this->assertNotEquals($this->entity->uuid()$duplicate->uuid());
    $this->assertSame($new_uuid$duplicate->uuid());
  }

  /** * @covers ::sort */
/** * {@inheritdoc} */
  public function getEntityType() {
    return $this->storage->getEntityType();
  }

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

  /** * {@inheritdoc} */
  public static function load($id) {
    return View::load($id);
  }

  /** * {@inheritdoc} */

  protected $theme;

  /** * {@inheritdoc} */
  public function createDuplicate() {
    $duplicate = parent::createDuplicate();
    $duplicate->revision_id->value = NULL;
    $duplicate->id->value = NULL;
    return $duplicate;
  }

  /** * {@inheritdoc} */
  public function setTheme($theme) {
    $this->theme = $theme;
    return $this;
  }
Home | Imprint | This part of the site doesn't use cookies.