removeComponent example


  public function testEntityFormModeAlter() {
    // Create compact entity display.     EntityFormMode::create(['id' => 'entity_test.compact', 'targetEntityType' => 'entity_test'])->save();
    EntityFormDisplay::create([
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'compact',
      'status' => TRUE,
    ])->removeComponent('field_test_text')->save();

    // The field should be available on default form mode.     $entity1 = EntityTest::create([
      'name' => $this->randomString(),
    ]);
    $entity1->save();
    $this->drupalGet($entity1->toUrl('edit-form'));
    $this->assertSession()->elementExists('css', 'input[name="field_test_text[0][value]"]');

    // The field should be hidden on compact form mode.     // See: entity_test_entity_form_mode_alter().
    $this->assertSession()->elementExists('xpath', "//body[contains(@class, 'page-node-type-page')]");

    // Get the UUID.     $url = parse_url($this->getUrl());
    $paths = explode('/', $url['path']);
    $view_mode = array_pop($paths);
    $uuid = array_pop($paths);

    // Switch view mode. We'll remove the body from the teaser view mode.     \Drupal::service('entity_display.repository')
      ->getViewDisplay('node', 'page', 'teaser')
      ->removeComponent('body')
      ->save();

    $view_mode_edit = ['view_mode' => 'teaser'];
    $this->drupalGet('node/preview/' . $uuid . '/full');
    $this->submitForm($view_mode_edit, 'Switch');
    $this->assertSession()->responseContains('view-mode-teaser');
    $this->assertSession()->pageTextNotContains($edit[$body_key]);

    // Check that the title, body and term fields are displayed with the     // values after going back to the content edit page.     $this->clickLink('Back to content editing');
    
    $value = mt_rand(1, 127);
    $edit = ["{$field_name}[0][value]" => $value];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('entity_test_rev ' . $id . ' has been updated.');
    $storage->resetCache([$id]);
    $entity = $storage->load($id);
    $this->assertEquals($value$entity->{$field_name}->value, 'Field value was updated');

    // Set the field back to hidden.     \Drupal::service('entity_display.repository')
      ->getFormDisplay($entity_type$this->field->getTargetBundle())
      ->removeComponent($this->field->getName())
      ->save();

    // Create a new revision.     $edit = ['revision' => TRUE];
    $this->drupalGet($entity_type . '/manage/' . $id . '/edit');
    $this->submitForm($edit, 'Save');

    // Check that the expected value has been carried over to the new revision.     $storage->resetCache([$id]);
    $entity = $storage->load($id);
    $this->assertEquals($value$entity->{$field_name}->value, 'New revision has the expected value for the field with the Hidden widget');
  }

  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $uuid = NULL) {
    $this->region = $region;
    $this->uuid = $uuid;
    return parent::buildForm($form$form_state$section_storage$delta);
  }

  /** * {@inheritdoc} */
  protected function handleSectionStorage(SectionStorageInterface $section_storage, FormStateInterface $form_state) {
    $section_storage->getSection($this->delta)->removeComponent($this->uuid);
  }

}
parent::setUpFixtures();
  }

  /** * Tests the default rendered entity output. */
  public function testRenderedEntityWithoutField() {
    \Drupal::currentUser()->setAccount($this->user);

    EntityViewDisplay::load('entity_test.entity_test.foobar')
      ->removeComponent('test_field')
      ->save();

    $view = Views::getView('test_field_entity_test_rendered');
    $build = [
      '#type' => 'view',
      '#name' => 'test_field_entity_test_rendered',
      '#view' => $view,
      '#display_id' => 'default',
    ];
    $renderer = \Drupal::service('renderer');
    $renderer->renderPlain($build);
    
    $display_repository->getViewDisplay('comment', 'comment')
      ->setComponent('links', ['weight' => 100])
      ->save();
    $this->drupalGet($this->node->toUrl());
    $element = $this->cssSelect('article.js-comment > div');
    // Get last child element.     $element = end($element);
    $this->assertNotEmpty($element->find('css', 'ul.links'), 'Last element is comment links.');

    // Make sure we can hide node links.     $display_repository->getViewDisplay('node', $this->node->bundle())
      ->removeComponent('links')
      ->save();
    $this->drupalGet($this->node->toUrl());
    $this->assertSession()->linkNotExists('1 comment');
    $this->assertSession()->linkNotExists('Add new comment');

    // Visit the full node, make sure there are links for the comment.     $this->drupalGet('node/' . $this->node->id());
    $this->assertSession()->pageTextContains($comment->getSubject());
    $this->assertSession()->linkExists('Reply');

    // Make sure we can hide comment links.
/** * {@inheritdoc} */
  public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
    parent::prepareFormDisplay($type$display);
    $source_field = $this->getSourceFieldDefinition($type)->getName();

    $display->setComponent($source_field[
      'type' => 'oembed_textfield',
      'weight' => $display->getComponent($source_field)['weight'],
    ]);
    $display->removeComponent('name');
  }

  /** * {@inheritdoc} */
  public function getProviders() {
    $configuration = $this->getConfiguration();
    return $configuration['providers'] ?: $this->getPluginDefinition()['providers'];
  }

  /** * {@inheritdoc} */
'type' => 'string',
      'weight' => -5,
      'settings' => [
        'link_to_entity' => FALSE,
      ],
      'third_party_settings' => [],
      'region' => 'content',
    ];
    $this->assertEquals($expected$display->getComponents());

    // Check that a component can be removed.     $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');
    
      // mode, and hide the field in all other form modes.       $entity_display_repository->getFormDisplay($entity_type$bundle)
        ->setComponent($field_name[
          'type' => 'comment_default',
          'weight' => 20,
        ])
        ->save();
      foreach ($entity_display_repository->getFormModes($entity_type) as $id => $form_mode) {
        $display = $entity_display_repository->getFormDisplay($entity_type$bundle$id);
        // Only update existing displays.         if ($display && !$display->isNew()) {
          $display->removeComponent($field_name)->save();
        }
      }

      // Entity view displays: assign widget settings for the default view       // mode, and hide the field in all other view modes.       $entity_display_repository->getViewDisplay($entity_type$bundle)
        ->setComponent($field_name[
          'label' => 'above',
          'type' => 'comment_default',
          'weight' => 20,
          'settings' => ['view_mode' => $comment_view_mode],
        ])

    $media_type->save();

    $this->drupalGet('media/add/' . $media_type_id);

    // Make sure we have a vertical tab fieldset and 'Path' field.     $assert_session->elementContains('css', '.js-form-type-vertical-tabs #edit-path-0 summary', 'URL alias');
    $assert_session->fieldExists('path[0][alias]');

    // Disable the 'Path' field for this content type.     \Drupal::service('entity_display.repository')->getFormDisplay('media', $media_type_id, 'default')
      ->removeComponent('path')
      ->save();

    $this->drupalGet('media/add/' . $media_type_id);

    // See if the whole fieldset is gone now.     $assert_session->elementNotExists('css', '.js-form-type-vertical-tabs #edit-path-0');
    $assert_session->fieldNotExists('path[0][alias]');
  }

}
$entity->getPluginCollections()->willReturn([]);
    $entity->getTargetEntityTypeId()->willReturn('entity_test_with_bundle');
    $entity->getTargetBundle()->willReturn('target_bundle');

    // An initially hidden field, with a submitted region change.     $entity->getComponent('new_field_mismatch_type_visible')->willReturn([]);
    $field_values['new_field_mismatch_type_visible'] = [
      'weight' => 0,
      'type' => 'textfield',
      'region' => 'hidden',
    ];
    $entity->removeComponent('new_field_mismatch_type_visible')
      ->will(function Darray $args) use ($entity) {
        // On subsequent calls, getComponent() will return an empty array.         $entity->getComponent($args[0])->willReturn([]);
      })
      ->shouldBeCalled();

    // An initially visible field, with identical submitted values.     $entity->getComponent('field_visible_no_changes')
      ->willReturn([
        'weight' => 0,
        'type' => 'textfield',
        

    $this->submitForm($edit, 'Save settings');

    $this->drupalGet($admin_path);
    $this->submitForm([], 'Save settings');
    $this->assertSession()->pageTextContains("Saved $field_name configuration");
    $field = FieldConfig::loadByName('node', $this->contentType, $field_name);
    $this->assertEquals([]$field->getDefaultValueLiteral(), 'The default value was correctly saved.');

    // Check that the default widget is used when the field is hidden.     $display_repository->getFormDisplay($field->getTargetEntityTypeId()$field->getTargetBundle())
      ->removeComponent($field_name)
      ->save();
    $this->drupalGet($admin_path);
    $this->assertSession()->fieldValueEquals($element_id, '');
  }

  /** * Tests that deletion removes field storages and fields as expected. */
  public function testDeleteField() {
    // Create a new field.     $bundle_path1 = 'admin/structure/types/manage/' . $this->contentType;
    
/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $region = $this->getSelectedRegion($form_state);
    $delta = $this->getSelectedDelta($form_state);
    $original_section = $this->sectionStorage->getSection($this->delta);
    $component = $original_section->getComponent($this->uuid);
    $section = $this->sectionStorage->getSection($delta);
    if ($delta !== $this->delta) {
      // Remove component from old section and add it to the new section.       $original_section->removeComponent($this->uuid);
      $section->insertComponent(0, $component);
    }
    $component->setRegion($region);
    foreach ($form_state->getValue('components') as $uuid => $component_info) {
      $section->getComponent($uuid)->setWeight($component_info['weight']);
    }
    $this->layoutTempstore->set($this->sectionStorage);
  }

  /** * Ajax callback for the region select element. * * @param array $form * The form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * * @return array * The components wrapper render array. */

  public function setCommentSubject($enabled) {
    $form_display = $this->container->get('entity_display.repository')
      ->getFormDisplay('comment', 'comment');

    if ($enabled) {
      $form_display->setComponent('subject', [
        'type' => 'string_textfield',
      ]);
    }
    else {
      $form_display->removeComponent('subject');
    }
    $form_display->save();
  }

  /** * Sets the value governing the previewing mode for the comment form. * * @param int $mode * The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED. * @param string $field_name * (optional) Field name through which the comment should be posted. * Defaults to 'comment'. */

  protected function getSingleFieldDisplay($entity$field_name$display_options) {
    if (is_string($display_options)) {
      // View mode: use the Display configured for the view mode.       $view_mode = $display_options;
      $display = EntityViewDisplay::collectRenderDisplay($entity$view_mode);
      // Hide all fields except the current one.       foreach (array_keys($entity->getFieldDefinitions()) as $name) {
        if ($name != $field_name) {
          $display->removeComponent($name);
        }
      }
    }
    else {
      // Array of custom display options: use a runtime Display for the       // '_custom' view mode. Persist the displays created, to reduce the number       // of objects (displays and formatter plugins) created when rendering a       // series of fields individually for cases such as views tables.       $entity_type_id = $entity->getEntityTypeId();
      $bundle = $entity->bundle();
      $key = $entity_type_id . ':' . $bundle . ':' . $field_name . ':' . Crypt::hashBase64(serialize($display_options));
      
Home | Imprint | This part of the site doesn't use cookies.