Verified Commit 328648c5 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3365239 by ksere, bcgreen, lauriii, joey-santiago, Chris Matthews,...

Issue #3365239 by ksere, bcgreen, lauriii, joey-santiago, Chris Matthews, kevinquillen, michaellenahan, onfire84, Mysdiir: Status message confirming field deletion incorrectly references 'content type'
parent b07fdfae
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for core/modules/comment/tests/src/Functional/CommentNonNodeTest.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -477,7 +477,7 @@ public function testCommentFunctionality() {

    $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
    $this->assertSession()->fieldValueEquals('comment_body[0][value]', '');
    $this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings');
    $this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings', 'comment type');
    $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
    $this->assertSession()->fieldNotExists('comment_body[0][value]');
    // Set subject field to autogenerate it.
+40 −12
Changes for core/modules/field_ui/src/Form/FieldConfigDeleteForm.php: 40 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\field_ui\FieldUI;
@@ -29,9 +30,16 @@ class FieldConfigDeleteForm extends EntityDeleteForm {
   *
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface|null $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info) {
  public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info, ?EntityTypeManagerInterface $entity_type_manager = NULL) {
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    if (!$entity_type_manager) {
      @trigger_error('Calling ' . __METHOD__ . '() without the $entity_type_manager argument is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3396525', E_USER_DEPRECATED);
      $entity_type_manager = \Drupal::service('entity_type.manager');
    }
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
@@ -39,7 +47,8 @@ public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_in
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.bundle.info')
      $container->get('entity_type.bundle.info'),
      $container->get('entity_type.manager'),
    );
  }

@@ -93,25 +102,44 @@ public function getCancelUrl() {
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $field_storage = $this->entity->getFieldStorageDefinition();
    $bundles = $this->entityTypeBundleInfo->getBundleInfo($this->entity->getTargetEntityTypeId());
    $bundle_label = $bundles[$this->entity->getTargetBundle()]['label'];
    $target_entity_type_id = $this->entity->getTargetEntityTypeId();
    $target_bundle = $this->entity->getTargetBundle();
    $target_entity_definition = $this->entityTypeManager->getDefinition($target_entity_type_id);
    $target_entity_bundle_entity_type_id = $target_entity_definition->getBundleEntityType();
    if (empty($target_entity_bundle_entity_type_id)) {
      $source_label = $this->t('entity type');
    }
    else {
      $target_entity_bundle_entity_type_definition = $this->entityTypeManager->getDefinition($target_entity_bundle_entity_type_id);
      $source_label = strtolower($target_entity_bundle_entity_type_definition->getLabel());
    }
    $bundles = $this->entityTypeBundleInfo->getBundleInfo($target_entity_type_id);
    $bundle_label = $bundles[$target_bundle]['label'];

    if ($field_storage && !$field_storage->isLocked()) {
      $this->entity->delete();
      $this->messenger()->addStatus($this->t('The field %field has been deleted from the %type content type.', ['%field' => $this->entity->label(), '%type' => $bundle_label]));
      $this->messenger()->addStatus($this->t('The field %field has been deleted from the %type %source_label.', [
        '%field' => $this->entity->label(),
        '%type' => $bundle_label,
        '%source_label' => $source_label,
      ]));
    }
    else {
      $this->messenger()->addError($this->t('There was a problem removing the %field from the %type content type.', ['%field' => $this->entity->label(), '%type' => $bundle_label]));
      $this->messenger()->addError($this->t('There was a problem removing the %field from the %type %source_label.', [
        '%field' => $this->entity->label(),
        '%type' => $bundle_label,
        '%source_label' => $source_label,
      ]));
    }

    $form_state->setRedirectUrl($this->getCancelUrl());

    // Fields are purged on cron. However field module prevents disabling modules
    // when field types they provided are used in a field until it is fully
    // purged. In the case that a field has minimal or no content, a single call
    // to field_purge_batch() will remove it from the system. Call this with a
    // low batch limit to avoid administrators having to wait for cron runs when
    // removing fields that meet this criteria.
    // Fields are purged on cron. However field module prevents disabling
    // modules when field types they provided are used in a field until it is
    // fully purged. In the case that a field has minimal or no content, a
    // single call to field_purge_batch() will remove it from the system. Call
    // this with a low batch limit to avoid administrators having to wait for
    // cron runs when removing fields that meet this criteria.
    field_purge_batch(10);
  }

+2 −2
Changes for core/modules/field_ui/tests/src/Functional/FieldUIDeleteTest.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -113,7 +113,7 @@ public function testDeleteField() {
    $this->assertSession()->pageTextNotContains('test_view_field_delete');

    // Delete the first field.
    $this->fieldUIDeleteField($bundle_path1, "node.$type_name1.$field_name", $field_label, $type_name1);
    $this->fieldUIDeleteField($bundle_path1, "node.$type_name1.$field_name", $field_label, $type_name1, 'content type');

    // Check that the field was deleted.
    $this->assertNull(FieldConfig::loadByName('node', $type_name1, $field_name), 'Field was deleted.');
@@ -129,7 +129,7 @@ public function testDeleteField() {
    $this->assertSession()->elementNotExists('css', '#edit-entity-deletes');

    // Delete the second field.
    $this->fieldUIDeleteField($bundle_path2, "node.$type_name2.$field_name", $field_label, $type_name2);
    $this->fieldUIDeleteField($bundle_path2, "node.$type_name2.$field_name", $field_label, $type_name2, 'content type');

    // Check that the field was deleted.
    $this->assertNull(FieldConfig::loadByName('node', $type_name2, $field_name), 'Field was deleted.');
+1 −1
Changes for core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -275,7 +275,7 @@ public function testDeleteTaxonomyField() {
    $this->fieldUIAddNewField($bundle_path, $this->fieldNameInput, $this->fieldLabel);

    // Delete the field.
    $this->fieldUIDeleteField($bundle_path, "taxonomy_term.tags.$this->fieldName", $this->fieldLabel, 'Tags');
    $this->fieldUIDeleteField($bundle_path, "taxonomy_term.tags.$this->fieldName", $this->fieldLabel, 'Tags', 'taxonomy vocabulary');

    // Check that the field was deleted.
    $this->assertNull(FieldConfig::loadByName('taxonomy_term', 'tags', $this->fieldName), 'Field was deleted.');
+4 −2
Changes for core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -165,8 +165,10 @@ public function fieldUIAddExistingField($bundle_path, $existing_storage_name, $l
   *   The label of the field.
   * @param string $bundle_label
   *   The label of the bundle.
   * @param string $source_label
   *   (optional) The label of the source entity type bundle.
   */
  public function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_label) {
  public function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_label, string $source_label = '') {
    // Display confirmation form.
    $this->drupalGet("$bundle_path/fields/$field_name/delete");
    $this->assertSession()->pageTextContains("Are you sure you want to delete the field $label");
@@ -176,7 +178,7 @@ public function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_la

    // Submit confirmation form.
    $this->submitForm([], 'Delete');
    $this->assertSession()->pageTextContains("The field $label has been deleted from the $bundle_label content type.");
    $this->assertSession()->pageTextContains("The field $label has been deleted from the $bundle_label $source_label");

    // Check that the field does not appear in the overview form.
    $xpath = $this->assertSession()->buildXPathQuery('//table[@id="field-overview"]//span[@class="label-field" and text()= :label]', [