loadByName example

$this->user = $this->createUser();
    \Drupal::service('current_user')->setAccount($this->user);
  }

  /** * Tests that field overrides work as expected. */
  public function testFieldOverrides() {
    if (!NodeType::load('ponies')) {
      NodeType::create(['name' => 'Ponies', 'type' => 'ponies'])->save();
    }
    $override = BaseFieldOverride::loadByName('node', 'ponies', 'uid');
    if ($override) {
      $override->delete();
    }
    $uid_field = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('node')['uid'];
    $config = $uid_field->getConfig('ponies');
    $config->save();
    $this->assertEquals('Drupal\node\Entity\Node::getDefaultEntityOwner', $config->get('default_value_callback'));
    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::create(['type' => 'ponies']);
    $owner = $node->getOwner();
    $this->assertInstanceOf(UserInterface::class$owner);
    
$this->vocabulary = $this->createVocabulary();

    $field_name = 'taxonomy_' . $this->vocabulary->id();

    $handler_settings = [
      'target_bundles' => [
        $this->vocabulary->id() => $this->vocabulary->id(),
      ],
      'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $this->field = FieldConfig::loadByName('node', 'article', $field_name);

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('node', 'article')
      ->setComponent($field_name[
        'type' => 'options_select',
      ])
      ->save();
    $display_repository->getViewDisplay('node', 'article')
      ->setComponent($field_name[
        'type' => 'entity_reference_label',
      ])

  public function postComment(?EntityInterface $entity$comment$subject = '', $contact = NULL) {
    $edit = [];
    $edit['comment_body[0][value]'] = $comment;

    $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment');
    $preview_mode = $field->getSetting('preview');

    // Must get the page before we test for fields.     if ($entity !== NULL) {
      $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment');
    }

    // Determine the visibility of subject form field.     $display_repository = $this->container->get('entity_display.repository');
    if ($display_repository->getFormDisplay('comment', 'comment')->getComponent('subject')) {
      // Subject input allowed.

  protected function removeSectionField($entity_type_id$bundle$field_name) {
    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
    $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());
    $query = $storage->getQuery()
      ->condition('targetEntityType', $this->getTargetEntityTypeId())
      ->condition('bundle', $this->getTargetBundle())
      ->condition('mode', $this->getMode(), '<>')
      ->condition('third_party_settings.layout_builder.allow_custom', TRUE);
    $enabled = (bool) $query->count()->execute();
    if (!$enabled && $field = FieldConfig::loadByName($entity_type_id$bundle$field_name)) {
      $field->delete();
    }
  }

  /** * Adds a layout section field to a given bundle. * * @param string $entity_type_id * The entity type ID. * @param string $bundle * The bundle. * @param string $field_name * The name for the layout section field. */
/** * Tests that the default 'comment_body' field is correctly added. */
  public function testCommentDefaultFields() {
    // Do not make assumptions on default node types created by the test     // installation profile, and create our own.     $this->drupalCreateContentType(['type' => 'test_node_type']);
    $this->addDefaultCommentField('node', 'test_node_type');

    // Check that the 'comment_body' field is present on the comment bundle.     $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
    $this->assertNotEmpty($field, 'The comment_body field is added when a comment bundle is created');

    $field->delete();

    // Check that the 'comment_body' field is not deleted since it is persisted     // even if it has no fields.     $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
    $this->assertInstanceOf(FieldStorageConfig::class$field_storage);

    // Create a new content type.     $type_name = 'test_node_type_2';
    
$referencing_entity->save();
    $this->assertEquals($published_node->id()$referencing_entity->field_test[0]->target_id);
    $this->assertEquals($unpublished_node->id()$referencing_entity->field_test[1]->target_id);
    $this->assertEquals($different_bundle_node->id()$referencing_entity->field_test[2]->target_id);
    $this->assertEquals($deleted_node->id()$referencing_entity->field_test[3]->target_id);

    $violations = $referencing_entity->field_test->validate();
    $this->assertCount(0, $violations);

    // Remove one of the referenceable bundles and check that a pre-existing node     // of that bundle can not be referenced anymore.     $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test');
    $field->setSetting('handler_settings', ['target_bundles' => ['article']]);
    $field->save();
    $referencing_entity = $this->reloadEntity($referencing_entity);

    $violations = $referencing_entity->field_test->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals(t('This entity (%type: %id) cannot be referenced.', [
      '%type' => 'node',
      '%id' => $different_bundle_node->id(),
    ])$violations[0]->getMessage());

    
    if ($this->profile != 'standard') {
      $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
      ViewTestData::createTestViews(static::class['node_test_views']);
    }

    // Add two new languages.     ConfigurableLanguage::createFromLangcode('fr')->save();
    ConfigurableLanguage::createFromLangcode('es')->save();

    // Make the body field translatable. The title is already translatable by     // definition.     $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $field_storage->setTranslatable(TRUE);
    $field_storage->save();

    // Set up node titles. They should not include the words "French",     // "English", or "Spanish", as there is a language field in the view     // that prints out those words.     $this->nodeTitles = [
      LanguageInterface::LANGCODE_NOT_SPECIFIED => [
        'First node und',
      ],
      'es' => [
        

  public function buildForm(array $form, FormStateInterface $form_state) {
    $comment_count = $this->entityTypeManager->getStorage('comment')->getQuery()
      ->accessCheck(FALSE)
      ->condition('comment_type', $this->entity->id())
      ->count()
      ->execute();
    $entity_type = $this->entity->getTargetEntityTypeId();
    $caption = '';
    foreach (array_keys($this->commentManager->getFields($entity_type)) as $field_name) {
      /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
      if (($field_storage = FieldStorageConfig::loadByName($entity_type$field_name)) && $field_storage->getSetting('comment_type') == $this->entity->id() && !$field_storage->isDeleted()) {
        $caption .= '<p>' . $this->t('%label is used by the %field field on your site. You can not remove this comment type until you have removed the field.', [
          '%label' => $this->entity->label(),
          '%field' => $field_storage->label(),
        ]) . '</p>';
      }
    }

    if ($comment_count) {
      $caption .= '<p>' . $this->formatPlural($comment_count, '%label is used by 1 comment on your site. You can not remove this comment type until you have removed all of the %label comments.', '%label is used by @count comments on your site. You may not remove %label until you have removed all of the %label comments.', ['%label' => $this->entity->label()]) . '</p>';
    }
    if ($caption) {
      
$editor_dom = $this->getEditorDataAsDom();
    $this->assertEquals('<a href="https://example.com">Llamas are the most awesome ever</a>', $editor_dom->getElementsByTagName('drupal-media')->item(0)->getAttribute('data-caption'));
  }

  /** * Tests that the image media source's alt_field being disabled is respected. * * @see \Drupal\Tests\ckeditor5\Functional\MediaEntityMetadataApiTest::testApi() */
  public function testAltDisabled(): void {
    // Disable the alt field for image media.     FieldConfig::loadByName('media', 'image', 'field_media_image')
      ->setSetting('alt_field', FALSE)
      ->save();

    $assert_session = $this->assertSession();
    $this->drupalGet($this->host->toUrl('edit-form'));
    $this->waitForEditor();
    // Wait for the media preview to load.     $this->assertNotEmpty($assert_session->waitForElementVisible('css', '.ck-widget.drupal-media img'));
    // Test that by default no alt attribute is present on the drupal-media     // element.     $this->assertSourceAttributeSame('alt', NULL);
    

  protected function setUp($import_test_views = TRUE, $modules = ['block_content_test_views']): void {
    parent::setUp($import_test_views$modules);

    // Add two new languages.     ConfigurableLanguage::createFromLangcode('fr')->save();
    ConfigurableLanguage::createFromLangcode('es')->save();

    // Make the body field translatable. The info is already translatable by     // definition.     $field_storage = FieldStorageConfig::loadByName('block_content', 'body');
    $field_storage->setTranslatable(TRUE);
    $field_storage->save();

    // Set up block_content infos.     $this->blockContentInfos = [
      'en' => 'Food in Paris',
      'es' => 'Comida en Paris',
      'fr' => 'Nourriture en Paris',
    ];

    // Create block_content with translations.

  protected function assertFieldStorageLangcode(FieldableEntityInterface $entity, string $message = ''): void {
    $status = TRUE;
    $entity_type = $entity->getEntityTypeId();
    $id = $entity->id();
    $langcode = $entity->getUntranslated()->language()->getId();
    $fields = [$this->fieldName, $this->untranslatableFieldName];
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = \Drupal::entityTypeManager()->getStorage($entity_type)->getTableMapping();

    foreach ($fields as $field_name) {
      $field_storage = FieldStorageConfig::loadByName($entity_type$field_name);
      $table = $table_mapping->getDedicatedDataTableName($field_storage);

      $record = \Drupal::database()
        ->select($table, 'f')
        ->fields('f')
        ->condition('f.entity_id', $id)
        ->condition('f.revision_id', $id)
        ->execute()
        ->fetchObject();

      if ($record->langcode != $langcode) {
        
/** * Tests that importing list_float fields works. */
  public function testImport() {
    $field_name = 'field_options_float';
    $type = 'options_install_test';

    // Test the results on installing options_config_install_test. All the     // necessary configuration for this test is created by installing that     // module.     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
    $this->assertSame($array = ['0' => 'Zero', '0.5' => 'Point five']$field_storage->getSetting('allowed_values'));

    $admin_path = 'admin/structure/types/manage/' . $type . '/fields/node.' . $type . '.' . $field_name . '/storage';

    // Export active config to sync.     $this->copyConfig($this->container->get('config.storage')$this->container->get('config.storage.sync'));

    // Set the active to not use dots in the allowed values key names.     $edit = ['settings[allowed_values]' => "0|Zero\n1|One"];
    $this->drupalGet($admin_path);
    $this->submitForm($edit, 'Save field settings');
    
\Drupal::entityTypeManager()->getStorage('comment_type')->resetCache(['foo']);
    $comment_type = CommentType::load('foo');
    $this->assertEquals('node', $comment_type->getTargetEntityTypeId());
  }

  /** * Tests editing a comment type using the UI. */
  public function testCommentTypeEditing() {
    $this->drupalLogin($this->adminUser);

    $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
    $this->assertEquals('Comment', $field->getLabel(), 'Comment body field was found.');

    // Change the comment type name.     $this->drupalGet('admin/structure/comment');
    $edit = [
      'label' => 'Bar',
    ];
    $this->drupalGet('admin/structure/comment/manage/comment');
    $this->submitForm($edit, 'Save');

    $this->drupalGet('admin/structure/comment');
    
public function testListingFieldsPage() {
    // Create a content type.     $node_type = NodeType::create([
      'type' => 'basic',
      'name' => 'Basic',
    ]);
    $node_type->save();

    $field = FieldConfig::create([
      // The field storage is guaranteed to exist because it is supplied by the       // node module.       'field_storage' => FieldStorageConfig::loadByName('node', 'body'),
      'bundle' => $node_type->id(),
      'label' => 'Body',
      'settings' => ['display_summary' => FALSE],
    ]);
    $field->save();

    $this->drupalGet('admin/config/regional/config-translation/node_fields');
    $this->assertSession()->pageTextContains('Body');
    $this->assertSession()->pageTextContains('Basic');
    $this->assertSession()->linkByHrefExists('admin/structure/types/manage/basic/fields/node.basic.body/translate');
  }

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

    // Set "Basic page" content type to use multilingual support.     $edit = [
      'language_configuration[language_alterable]' => TRUE,
    ];
    $this->drupalGet('admin/structure/types/manage/page');
    $this->submitForm($edit, 'Save content type');
    $this->assertSession()->pageTextContains("The content type Basic page has been updated.");

    // Make node body translatable.     $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $field_storage->setTranslatable(TRUE);
    $field_storage->save();
  }

  /** * Tests whether field languages are correctly set through the node form. */
  public function testMultilingualNodeForm() {
    // Create "Basic page" content.     $langcode = language_get_default_langcode('node', 'page');
    $title_key = 'title[0][value]';
    
Home | Imprint | This part of the site doesn't use cookies.