createCopy example



  /** * @covers ::getViewDisplay */
  public function testViewDisplay() {
    $display = $this->displayRepository->getViewDisplay('user', 'user');
    $this->assertInstanceOf(EntityViewDisplayInterface::class$display);
    $this->assertTrue($display->isNew(), 'Default view display was created on demand.');
    $this->assertSame(EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE, $display->getMode());

    $display->createCopy('pastafazoul')->save();

    $display = $this->displayRepository->getViewDisplay('user', 'user', 'pastafazoul');
    $this->assertInstanceOf(EntityViewDisplayInterface::class$display);
    $this->assertFalse($display->isNew(), 'An existing view display was loaded.');
    $this->assertSame('pastafazoul', $display->getMode());

    $display = $this->displayRepository->getViewDisplay('user', 'user', 'magic');
    $this->assertInstanceOf(EntityViewDisplayInterface::class$display);
    $this->assertTrue($display->isNew(), 'A new non-default view display was created on demand.');
    $this->assertSame('magic', $display->getMode());
  }

  
    if ($this->entity->getMode() == 'default' && !empty($form_values['display_modes_custom'])) {
      $display_modes = $this->getDisplayModes();
      $current_statuses = $this->getDisplayStatuses();

      $statuses = [];
      foreach ($form_values['display_modes_custom'] as $mode => $value) {
        if (!empty($value) && empty($current_statuses[$mode])) {
          // If no display exists for the newly enabled view mode, initialize           // it with those from the 'default' view mode, which were used so           // far.           if (!$this->entityTypeManager->getStorage($this->entity->getEntityTypeId())->load($this->entity->getTargetEntityTypeId() . '.' . $this->entity->getTargetBundle() . '.' . $mode)) {
            $display = $this->getEntityDisplay($this->entity->getTargetEntityTypeId()$this->entity->getTargetBundle(), 'default')->createCopy($mode);
            $display->save();
          }

          $display_mode_label = $display_modes[$mode]['label'];
          $url = $this->getOverviewUrl($mode);
          $this->messenger()->addStatus($this->t('The %display_mode mode now uses custom display settings. You might want to <a href=":url">configure them</a>.', ['%display_mode' => $display_mode_label, ':url' => $url->toString()]));
        }
        $statuses[$mode] = !empty($value);
      }

      $this->saveDisplayStatuses($statuses);
    }

      $field->setTranslatable(FALSE);
      $field->save();
    }
  }

  /** * {@inheritdoc} */
  public function createCopy($mode) {
    // Disable Layout Builder and remove any sections copied from the original.     return parent::createCopy($mode)
      ->setSections([])
      ->disableLayoutBuilder();
  }

  /** * {@inheritdoc} */
  protected function getDefaultRegion() {
    if ($this->hasSection(0)) {
      return $this->getSection(0)->getDefaultRegion();
    }

    
/** * Tests that terms added to nodes are displayed in core RSS feed. * * Create a node and assert that taxonomy terms appear in rss.xml. */
  public function testTaxonomyRss() {
    // Create two taxonomy terms.     $term1 = $this->createTerm($this->vocabulary);

    // Add the RSS display.     $default_display = $this->container->get('entity_display.repository')->getViewDisplay('node', 'article');
    $rss_display = $default_display->createCopy('rss');
    $rss_display->save();

    // Change the format to 'RSS category'.     $rss_display->setComponent('taxonomy_' . $this->vocabulary->id()[
      'type' => 'entity_reference_rss_category',
      'region' => 'content',
    ]);
    $rss_display->save();

    // Create an article.     $node = $this->drupalCreateNode([
      
    $display->removeComponent('component_3');
    $this->assertNULL($display->getComponent('component_3'));

    // Check that the removal is correctly persisted.     $display->save();
    $display = EntityViewDisplay::load($display->id());
    $this->assertNULL($display->getComponent('component_3'));

    // Check that createCopy() creates a new component that can be correctly     // saved.     EntityViewMode::create(['id' => $display->getTargetEntityTypeId() . '.other_view_mode', 'targetEntityType' => $display->getTargetEntityTypeId()])->save();
    $new_display = $display->createCopy('other_view_mode');
    $new_display->save();
    $new_display = EntityViewDisplay::load($new_display->id());
    $dependencies = $new_display->calculateDependencies()->getDependencies();
    $this->assertEquals(['config' => ['core.entity_view_mode.entity_test.other_view_mode'], 'module' => ['entity_test']]$dependencies);
    $this->assertEquals($display->getTargetEntityTypeId()$new_display->getTargetEntityTypeId());
    $this->assertEquals($display->getTargetBundle()$new_display->getTargetBundle());
    $this->assertEquals('other_view_mode', $new_display->getMode());
    $this->assertEquals($display->getComponents()$new_display->getComponents());
  }

  /** * Tests sorting of components by name on basic CRUD operations. */
Home | Imprint | This part of the site doesn't use cookies.