node_add_body_field example

'scheme' => 'public',
        'directory' => '',
        'status' => TRUE,
      ],
    ]);
    $editor->save();
    $this->editor = $editor;

    // Create a node type for testing.     $type = NodeType::create(['type' => 'page', 'name' => 'page']);
    $type->save();
    node_add_body_field($type);
    $this->installEntitySchema('user');
  }

  /** * Tests that editor image dialog works as expected. */
  public function testEditorImageDialog() {
    $input = [
      'editor_object' => [
        'src' => '/sites/default/files/inline-images/some-file.png',
        'alt' => 'fda',
        
// Set up text editor.     $editor = Editor::create([
      'format' => 'filtered_html',
      'editor' => 'unicorn',
    ]);
    $editor->save();

    // Create a node type for testing.     $type = NodeType::create(['type' => 'page', 'name' => 'page']);
    $type->save();
    node_add_body_field($type);
    FieldStorageConfig::create([
      'field_name' => 'description',
      'entity_type' => 'node',
      'type' => 'editor_test_text_long',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ])->save();
    FieldConfig::create([
      'field_name' => 'description',
      'entity_type' => 'node',
      'bundle' => 'page',
      'label' => 'Description',
    ])

class EntityNodeType extends EntityConfigBase {

  /** * {@inheritdoc} */
  public function import(Row $row, array $old_destination_id_values = []) {
    $entity_ids = parent::import($row$old_destination_id_values);
    if ($row->getDestinationProperty('create_body')) {
      $node_type = $this->storage->load(reset($entity_ids));
      node_add_body_field($node_type$row->getDestinationProperty('create_body_label'));
    }
    return $entity_ids;
  }

}
$this->assertEquals($options$display->getComponent($field_name));
    }
  }

  /** * Tests deleting a bundle. */
  public function testDeleteBundle() {
    // Create a node bundle, display and form display object.     $type = NodeType::create(['type' => 'article']);
    $type->save();
    node_add_body_field($type);
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getViewDisplay('node', 'article')->save();
    $display_repository->getFormDisplay('node', 'article')->save();

    // Delete the bundle.     $type->delete();
    $display = EntityViewDisplay::load('node.article.default');
    $this->assertFalse((bool) $display);
    $form_display = EntityFormDisplay::load('node.article.default');
    $this->assertFalse((bool) $form_display);
  }
$this->container->get('extension.list.theme')
    );
  }

  public function testRecreateEntity() {
    $type_name = mb_strtolower($this->randomMachineName(16));
    $content_type = NodeType::create([
      'type' => $type_name,
      'name' => 'Node type one',
    ]);
    $content_type->save();
    node_add_body_field($content_type);
    /** @var \Drupal\Core\Config\StorageInterface $active */
    $active = $this->container->get('config.storage');
    /** @var \Drupal\Core\Config\StorageInterface $sync */
    $sync = $this->container->get('config.storage.sync');

    $config_name = $content_type->getEntityType()->getConfigPrefix() . '.' . $content_type->id();
    $this->copyConfig($active$sync);

    // Delete the content type. This will also delete a field storage, a field,     // an entity view display and an entity form display.     $content_type->delete();
    
$type->set('type', trim($type->id()));
    $type->set('name', trim($type->label()));

    $status = $type->save();

    $t_args = ['%name' => $type->label()];

    if ($status == SAVED_UPDATED) {
      $this->messenger()->addStatus($this->t('The content type %name has been updated.', $t_args));
    }
    elseif ($status == SAVED_NEW) {
      node_add_body_field($type);
      $this->messenger()->addStatus($this->t('The content type %name has been added.', $t_args));
      $context = array_merge($t_args['link' => $type->toLink($this->t('View'), 'collection')->toString()]);
      $this->logger('node')->notice('Added content type %name.', $context);
    }

    $fields = $this->entityFieldManager->getFieldDefinitions('node', $type->id());
    // Update title field definition.     $title_field = $fields['title'];
    $title_label = $form_state->getValue('title_label');
    if ($title_field->getLabel() != $title_label) {
      $title_field->getConfig($type->id())->setLabel($title_label)->save();
    }
protected static $modules = ['node', 'filter'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->installConfig(['filter', 'node']);

    $node_type = NodeType::create(['type' => 'article', 'name' => 'Article']);
    $node_type->save();
    node_add_body_field($node_type);
  }

  /** * Creates a node, then tests the tokens generated from it. */
  public function testNodeTokenReplacement() {
    $url_options = [
      'absolute' => TRUE,
      'language' => $this->interfaceLanguage,
    ];

    

  public function testDisableFormatWithEditor() {
    $formats = ['monoceros' => 'Monoceros', 'tattoo' => 'Tattoo'];

    // Install the node module.     $this->container->get('module_installer')->install(['node']);
    $this->resetAll();
    // Create a new node type and attach the 'body' field to it.     $node_type = NodeType::create(['type' => mb_strtolower($this->randomMachineName()), 'name' => $this->randomString()]);
    $node_type->save();
    node_add_body_field($node_type$this->randomString());

    $permissions = ['administer filters', "edit any {$node_type->id()} content"];
    foreach ($formats as $format => $name) {
      // Create a format and add an editor to this format.       $this->addEditorToNewFormat($format$name);
      // Add permission for this format.       $permissions[] = "use text format $format";
    }

    // Create a node having the body format value 'monoceros'.     $node = Node::create([
      
$this->assertSession()->responseContains('<td class="type">' . $ampersand_as_code . '</td>');
    $this->assertSession()->responseContains('<td class="get">' . $ampersand . '</td>');
  }

  /** * Tests whether a field using a disabled format is rendered. */
  public function testDisabledFormat() {
    // Create a node type and add a standard body field.     $node_type = NodeType::create(['type' => mb_strtolower($this->randomMachineName())]);
    $node_type->save();
    node_add_body_field($node_type$this->randomString());

    // Create a text format with a filter that returns a static string.     $format = FilterFormat::create([
      'name' => $this->randomString(),
      'format' => $format_id = mb_strtolower($this->randomMachineName()),
    ]);
    $format->setFilterConfig('filter_static_text', ['status' => TRUE]);
    $format->save();

    // Create a new node of the new node type.     $node = Node::create([
      
 while (NodeType::load($id));
    }
    else {
      $id = $values['type'];
    }
    $values += [
      'type' => $id,
      'name' => $id,
    ];
    $type = NodeType::create($values);
    $status = $type->save();
    node_add_body_field($type);

    if ($this instanceof TestCase) {
      $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
    }
    else {
      $this->assertEquals(SAVED_NEW, $status(new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
    }

    return $type;
  }

}

  protected $defaultTheme = 'stark';

  /** * Tests token replacement for Views tokens supplied by the Node module. */
  public function testViewsTokenReplacement() {
    // Create the Article content type with a standard body field.     /** @var \Drupal\node\NodeTypeInterface $node_type */
    $node_type = NodeType::create(['type' => 'article', 'name' => 'Article']);
    $node_type->save();
    node_add_body_field($node_type);

    // Create a user and a node.     $account = $this->createUser();
    $body = $this->randomMachineName(32);
    $summary = $this->randomMachineName(16);

    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::create([
      'type' => 'article',
      'tnid' => 0,
      'uid' => $account->id(),
      
$this->installConfig(['field', 'node']);
  }

  /** * Tests node body field storage persistence even if there are no instances. */
  public function testFieldOverrides() {
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $this->assertNotEmpty($field_storage, 'Node body field storage exists.');
    $type = NodeType::create(['name' => 'Ponies', 'type' => 'ponies']);
    $type->save();
    node_add_body_field($type);
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $this->assertCount(1, $field_storage->getBundles(), 'Node body field storage is being used on the new node type.');
    $field = FieldConfig::loadByName('node', 'ponies', 'body');
    $field->delete();
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $this->assertCount(0, $field_storage->getBundles(), 'Node body field storage exists after deleting the only instance of a field.');
    \Drupal::service('module_installer')->uninstall(['node']);
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $this->assertNull($field_storage, 'Node body field storage does not exist after uninstalling the Node module.');
  }

}
Home | Imprint | This part of the site doesn't use cookies.