Unverified Commit 8e53c1ce authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3069925 by Lendude, Kova101, kndr, Krzysztof Domański: When...

Issue #3069925 by Lendude, Kova101, kndr, Krzysztof Domański: When target_bundles is not set it produces a PHP notice and causes unexpected error

(cherry picked from commit 0f0ebbec)
parent b2984279
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -298,16 +298,19 @@ protected function buildFilters(&$form, FormStateInterface $form_state) {
        $tag_field_name = key($tag_fields);
      }
      // Add the autocomplete textfield to the wizard.
      $target_bundles = $tag_fields[$tag_field_name]->getSetting('handler_settings')['target_bundles'];
      $form['displays']['show']['tagged_with'] = [
        '#type' => 'entity_autocomplete',
        '#title' => $this->t('tagged with'),
        '#target_type' => 'taxonomy_term',
        '#selection_settings' => ['target_bundles' => $target_bundles],
        '#tags' => TRUE,
        '#size' => 30,
        '#maxlength' => 1024,
      ];
      $target_bundles = $tag_fields[$tag_field_name]->getSetting('handler_settings')['target_bundles'] ?? FALSE;
      if (!$target_bundles) {
        $target_bundles = array_keys($this->bundleInfoService->getBundleInfo('taxonomy_term'));
      }
      $form['displays']['show']['tagged_with']['#selection_settings']['target_bundles'] = $target_bundles;
    }
  }

+38 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;

@@ -233,4 +234,41 @@ public function testTaggedWithByNodeType() {
    $this->assertSession()->fieldExists("show[tagged_with]");
  }

  /**
   * Tests that "tagged with" works with views entity reference.
   */
  public function testTaggedWithByViewReference() {
    Term::create(['name' => 'term1', 'vid' => 'views_testing_tags']);
    $tags_xpath = '//input[@name="show[tagged_with]"]';

    // If we add an instance of the tagging field to the second node type, the
    // "tagged with" form element should now appear for it too.
    FieldConfig::create([
      'field_name' => $this->tagFieldName,
      'entity_type' => 'node',
      'bundle' => $this->nodeTypeWithoutTags->id(),
      'settings' => [
        'handler' => 'views',
        'handler_settings' => [],
      ],
    ])->save();
    \Drupal::service('entity_display.repository')
      ->getFormDisplay('node', $this->nodeTypeWithoutTags->id())
      ->setComponent($this->tagFieldName, [
        'type' => 'entity_reference_autocomplete_tags',
      ])
      ->save();

    $view['show[type]'] = $this->nodeTypeWithTags->id();
    $this->drupalGet('admin/structure/views/add');
    $this->submitForm($view, 'Update "of type" choice');
    $this->assertNotEmpty($this->xpath($tags_xpath));
    $view['show[type]'] = $this->nodeTypeWithoutTags->id();
    $this->submitForm($view, 'Update "of type" choice (2)');
    $this->assertNotEmpty($this->xpath($tags_xpath));
    $this->submitForm(['show[tagged_with]' => 'term1'], 'Save and edit');
    $this->assertSession()->statusCodeEquals(200);
    $this->getSession()->getPage()->hasContent('Has taxonomy term (= term1)');
  }

}