本文整理汇总了PHP中Drupal\node\Entity\NodeType::create方法的典型用法代码示例。如果您正苦于以下问题:PHP NodeType::create方法的具体用法?PHP NodeType::create怎么用?PHP NodeType::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\node\Entity\NodeType
的用法示例。
在下文中一共展示了NodeType::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testExportWithReferences
/**
* Tests exportContentWithReferences().
*/
public function testExportWithReferences()
{
\Drupal::service('module_installer')->install(['node', 'default_content']);
\Drupal::service('router.builder')->rebuild();
$this->defaultContentManager = \Drupal::service('default_content.manager');
$user = User::create(['name' => 'my username']);
$user->save();
// Reload the user to get the proper casted values from the DB.
$user = User::load($user->id());
$node_type = NodeType::create(['type' => 'test']);
$node_type->save();
$node = Node::create(['type' => $node_type->id(), 'title' => 'test node', 'uid' => $user->id()]);
$node->save();
// Reload the node to get the proper casted values from the DB.
$node = Node::load($node->id());
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
\Drupal::service('rest.link_manager')->setLinkDomain(DefaultContentManager::LINK_DOMAIN);
$expected_node = $serializer->serialize($node, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$expected_user = $serializer->serialize($user, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$exported_by_entity_type = $this->defaultContentManager->exportContentWithReferences('node', $node->id());
// Ensure that the node type is not tryed to be exported.
$this->assertEqual(array_keys($exported_by_entity_type), ['node', 'user']);
// Ensure the right UUIDs are exported.
$this->assertEqual([$node->uuid()], array_keys($exported_by_entity_type['node']));
$this->assertEqual([$user->uuid()], array_keys($exported_by_entity_type['user']));
// Compare the actual serialized data.
$this->assertEqual(reset($exported_by_entity_type['node']), $expected_node);
$this->assertEqual(reset($exported_by_entity_type['user']), $expected_user);
}
示例2: testComment
/**
* Tests the normalization of comments.
*/
public function testComment()
{
$node_type = NodeType::create(['type' => 'example_type']);
$node_type->save();
$account = User::create(['name' => $this->randomMachineName()]);
$account->save();
// Add comment type.
$this->container->get('entity.manager')->getStorage('comment_type')->create(array('id' => 'comment', 'label' => 'comment', 'target_entity_type_id' => 'node'))->save();
$this->addDefaultCommentField('node', 'example_type');
$node = Node::create(['title' => $this->randomMachineName(), 'uid' => $account->id(), 'type' => $node_type->id(), 'status' => NODE_PUBLISHED, 'promote' => 1, 'sticky' => 0, 'body' => [['value' => $this->randomMachineName(), 'format' => $this->randomMachineName()]]]);
$node->save();
$parent_comment = Comment::create(array('uid' => $account->id(), 'subject' => $this->randomMachineName(), 'comment_body' => ['value' => $this->randomMachineName(), 'format' => NULL], 'entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment'));
$parent_comment->save();
$comment = Comment::create(array('uid' => $account->id(), 'subject' => $this->randomMachineName(), 'comment_body' => ['value' => $this->randomMachineName(), 'format' => NULL], 'entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'pid' => $parent_comment->id(), 'mail' => 'dries@drupal.org', 'homepage' => 'http://buytaert.net'));
$comment->save();
$original_values = $comment->toArray();
// Hostname will always be denied view access.
// No value will exist for name as this is only for anonymous users.
unset($original_values['hostname'], $original_values['name']);
$normalized = $this->serializer->normalize($comment, $this->format, ['account' => $account]);
// Assert that the hostname field does not appear at all in the normalized
// data.
$this->assertFalse(array_key_exists('hostname', $normalized), 'Hostname was not found in normalized comment data.');
/** @var \Drupal\comment\CommentInterface $denormalized_comment */
$denormalized_comment = $this->serializer->denormalize($normalized, 'Drupal\\comment\\Entity\\Comment', $this->format, ['account' => $account]);
// Before comparing, unset values that are expected to differ.
$denormalized_comment_values = $denormalized_comment->toArray();
unset($denormalized_comment_values['hostname'], $denormalized_comment_values['name']);
$this->assertEqual($original_values, $denormalized_comment_values, 'The expected comment values are restored after normalizing and denormalizing.');
}
示例3: addParagraphedContentType
/**
* Adds a content type with a Paragraphs field.
*
* @param string $content_type_name
* Content type name to be used.
* @param string $paragraphs_field_name
* Paragraphs field name to be used.
*/
protected function addParagraphedContentType($content_type_name, $paragraphs_field_name)
{
// Create the content type.
$node_type = NodeType::create(['type' => $content_type_name, 'name' => $content_type_name]);
$node_type->save();
$this->addParagraphsField($content_type_name, $paragraphs_field_name, 'node');
}
示例4: setUp
public function setUp() {
parent::setup();
$this->installConfig(array('pathauto', 'taxonomy', 'system', 'node'));
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installEntitySchema('taxonomy_term');
ConfigurableLanguage::createFromLangcode('fr')->save();
$this->installSchema('node', array('node_access'));
$this->installSchema('system', array('url_alias', 'sequences', 'router'));
$type = NodeType::create(['type' => 'page']);
$type->save();
node_add_body_field($type);
$this->nodePattern = $this->createPattern('node', '/content/[node:title]');
$this->userPattern = $this->createPattern('user', '/users/[user:name]');
\Drupal::service('router.builder')->rebuild();
$this->currentUser = entity_create('user', array('name' => $this->randomMachineName()));
$this->currentUser->save();
}
示例5: setUp
protected function setUp()
{
parent::setUp();
// Ensure the page node type exists.
NodeType::create(['type' => 'page', 'name' => 'page'])->save();
ViewTestData::createTestViews(get_class($this), array('field_test_views'));
}
示例6: testNodeTranslation
/**
* Tests the normalization of node translations.
*/
public function testNodeTranslation()
{
$node_type = NodeType::create(['type' => 'example_type']);
$node_type->save();
$this->container->get('content_translation.manager')->setEnabled('node', 'example_type', TRUE);
$user = User::create(['name' => $this->randomMachineName()]);
$user->save();
$node = Node::create(['title' => $this->randomMachineName(), 'uid' => $user->id(), 'type' => $node_type->id(), 'status' => NODE_PUBLISHED, 'langcode' => 'en', 'promote' => 1, 'sticky' => 0, 'body' => ['value' => $this->randomMachineName(), 'format' => $this->randomMachineName()], 'revision_log' => $this->randomString()]);
$node->addTranslation('de', ['title' => 'German title', 'body' => ['value' => $this->randomMachineName(), 'format' => $this->randomMachineName()]]);
$node->save();
$original_values = $node->toArray();
$translation = $node->getTranslation('de');
$original_translation_values = $node->getTranslation('en')->toArray();
$normalized = $this->serializer->normalize($node, $this->format);
$this->assertContains(['lang' => 'en', 'value' => $node->getTitle()], $normalized['title'], 'Original language title has been normalized.');
$this->assertContains(['lang' => 'de', 'value' => $translation->getTitle()], $normalized['title'], 'Translation language title has been normalized.');
/** @var \Drupal\node\NodeInterface $denormalized_node */
$denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\\node\\Entity\\Node', $this->format);
$this->assertSame($denormalized_node->language()->getId(), $denormalized_node->getUntranslated()->language()->getId(), 'Untranslated object is returned from serializer.');
$this->assertSame('en', $denormalized_node->language()->getId());
$this->assertTrue($denormalized_node->hasTranslation('de'));
$this->assertSame($node->getTitle(), $denormalized_node->getTitle());
$this->assertSame($translation->getTitle(), $denormalized_node->getTranslation('de')->getTitle());
$this->assertEquals($original_values, $denormalized_node->toArray(), 'Node values are restored after normalizing and denormalizing.');
$this->assertEquals($original_translation_values, $denormalized_node->getTranslation('en')->toArray(), 'Node values are restored after normalizing and denormalizing.');
}
示例7: testRecreateEntity
public function testRecreateEntity()
{
$type_name = Unicode::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();
$this->assertFalse($active->exists($config_name), 'Content type\'s old name does not exist active store.');
// Recreate with the same type - this will have a different UUID.
$content_type = NodeType::create(['type' => $type_name, 'name' => 'Node type two']);
$content_type->save();
node_add_body_field($content_type);
$this->configImporter->reset();
// A node type, a field, an entity view display and an entity form display
// will be recreated.
$creates = $this->configImporter->getUnprocessedConfiguration('create');
$deletes = $this->configImporter->getUnprocessedConfiguration('delete');
$this->assertEqual(5, count($creates), 'There are 5 configuration items to create.');
$this->assertEqual(5, count($deletes), 'There are 5 configuration items to delete.');
$this->assertEqual(0, count($this->configImporter->getUnprocessedConfiguration('update')), 'There are no configuration items to update.');
$this->assertIdentical($creates, array_reverse($deletes), 'Deletes and creates contain the same configuration names in opposite orders due to dependencies.');
$this->configImporter->import();
// Verify that there is nothing more to import.
$this->assertFalse($this->configImporter->reset()->hasUnprocessedConfigurationChanges());
$content_type = NodeType::load($type_name);
$this->assertEqual('Node type one', $content_type->label());
}
示例8: createNodeType
/**
* Creates a new node type.
*
* @param string $label
* The human-readable label of the type to create.
* @param string $machine_name
* The machine name of the type to create.
*
* @return NodeType
* The node type just created.
*/
protected function createNodeType($label, $machine_name)
{
/** @var NodeType $node_type */
$node_type = NodeType::create(['type' => $machine_name, 'label' => $label]);
$node_type->save();
return $node_type;
}
示例9: testRenameValidation
/**
* Tests configuration renaming validation.
*/
public function testRenameValidation()
{
// Create a test entity.
$test_entity_id = $this->randomMachineName();
$test_entity = entity_create('config_test', array('id' => $test_entity_id, 'label' => $this->randomMachineName()));
$test_entity->save();
$uuid = $test_entity->uuid();
// Stage the test entity and then delete it from the active storage.
$active = $this->container->get('config.storage');
$sync = $this->container->get('config.storage.sync');
$this->copyConfig($active, $sync);
$test_entity->delete();
// Create a content type with a matching UUID in the active storage.
$content_type = NodeType::create(['type' => Unicode::strtolower($this->randomMachineName(16)), 'name' => $this->randomMachineName(), 'uuid' => $uuid]);
$content_type->save();
// Confirm that the staged configuration is detected as a rename since the
// UUIDs match.
$this->configImporter->reset();
$expected = array('node.type.' . $content_type->id() . '::config_test.dynamic.' . $test_entity_id);
$renames = $this->configImporter->getUnprocessedConfiguration('rename');
$this->assertIdentical($expected, $renames);
// Try to import the configuration. We expect an exception to be thrown
// because the staged entity is of a different type.
try {
$this->configImporter->import();
$this->fail('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
} catch (ConfigImporterException $e) {
$this->pass('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
$expected = array(SafeMarkup::format('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', array('@old_type' => 'node_type', '@new_type' => 'config_test', '@old_name' => 'node.type.' . $content_type->id(), '@new_name' => 'config_test.dynamic.' . $test_entity_id)));
$this->assertEqual($expected, $this->configImporter->getErrors());
}
}
示例10: prepareTranslationSuggestions
/**
* Prepare a node to get suggestions from.
*
* Creates a node with two file fields. The first one is not translatable,
* the second one is. Both fields got two files attached, where one has
* translatable content (title and atl-text) and the other one not.
*
* @return object
* The node which is prepared with all needed fields for the suggestions.
*/
protected function prepareTranslationSuggestions()
{
// Create a content type with fields.
// Only the first field is a translatable reference.
$type = NodeType::create(['type' => $this->randomMachineName()]);
$type->save();
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager->setEnabled('node', $type->id(), TRUE);
$field1 = FieldStorageConfig::create(array('field_name' => 'field1', 'entity_type' => 'node', 'type' => 'entity_reference', 'cardinality' => -1, 'settings' => array('target_type' => 'node')));
$field1->save();
$field2 = FieldStorageConfig::create(array('field_name' => 'field2', 'entity_type' => 'node', 'type' => 'entity_reference', 'cardinality' => -1, 'settings' => array('target_type' => 'node')));
$field2->save();
// Create field instances on the content type.
FieldConfig::create(array('field_storage' => $field1, 'bundle' => $type->id(), 'label' => 'Field 1', 'translatable' => FALSE, 'settings' => array()))->save();
FieldConfig::create(array('field_storage' => $field2, 'bundle' => $type->id(), 'label' => 'Field 2', 'translatable' => TRUE, 'settings' => array()))->save();
// Create a translatable body field.
node_add_body_field($type);
$field = FieldConfig::loadByName('node', $type->id(), 'body');
$field->setTranslatable(TRUE);
$field->save();
// Create 4 nodes to be referenced.
$references = array();
for ($i = 0; $i < 4; $i++) {
$references[$i] = Node::create(array('title' => $this->randomMachineName(), 'body' => $this->randomMachineName(), 'type' => $type->id()));
$references[$i]->save();
}
// Create a node with two translatable and two non-translatable references.
$node = Node::create(array('title' => $this->randomMachineName(), 'type' => $type->id(), 'language' => 'en', 'body' => $this->randomMachineName(), $field1->getName() => array(array('target_id' => $references[0]->id()), array('target_id' => $references[1]->id())), $field2->getName() => array(array('target_id' => $references[2]->id()), array('target_id' => $references[3]->id()))));
$node->save();
$link = MenuLinkContent::create(['link' => [['uri' => 'entity:node/' . $node->id()]], 'title' => 'Node menu link', 'menu_name' => 'main']);
$link->save();
$node->link = $link;
return $node;
}
示例11: setUp
/**
* Set the default field storage backend for fields created during tests.
*/
protected function setUp()
{
parent::setUp();
// Create a node type for testing.
$type = NodeType::create(['type' => 'page', 'name' => 'page']);
$type->save();
}
示例12: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('node');
// Need at least one node type present.
NodeType::create(['type' => 'testnodetype', 'name' => 'Test node type'])->save();
}
示例13: setUp
/**
* Performs setup tasks before each individual test method is run.
*/
public function setUp()
{
parent::setUp('content_access');
// The parent method already installs most needed node and comment schemas,
// but here we also need the comment statistics.
$this->installSchema('comment', array('comment_entity_statistics'));
// Create a node type for testing.
$type = NodeType::create(array('type' => 'page', 'name' => 'page'));
$type->save();
// Create anonymous user role.
$role = Role::create(array('id' => 'anonymous', 'label' => 'anonymous'));
$role->save();
// Insert the anonymous user into the database, as the user table is inner
// joined by \Drupal\comment\CommentStorage.
User::create(array('uid' => 0, 'name' => ''))->save();
// Create a node with attached comment.
$this->nodes[0] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'test title'));
$this->nodes[0]->save();
$comment_type = CommentType::create(array('id' => 'comment', 'target_entity_type_id' => 'node'));
$comment_type->save();
$this->installConfig(array('comment'));
$this->addDefaultCommentField('node', 'page');
$comment = Comment::create(array('entity_type' => 'node', 'entity_id' => $this->nodes[0]->id(), 'field_name' => 'comment', 'body' => 'test body', 'comment_type' => $comment_type->id()));
$comment->save();
$this->comments[] = $comment;
$this->nodes[1] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'some title'));
$this->nodes[1]->save();
$this->nodes[2] = Node::create(array('status' => NODE_NOT_PUBLISHED, 'type' => 'page', 'title' => 'other title'));
$this->nodes[2]->save();
// Also index users, to verify that they are unaffected by the processor.
$this->index->set('datasources', array('entity:comment', 'entity:node', 'entity:user'));
$this->index->save();
$this->index = entity_load('search_api_index', $this->index->id(), TRUE);
}
示例14: testNodeType
/**
* Tests the node type config entity.
*/
public function testNodeType()
{
// Create an english test entity.
$node_type = NodeType::create(array('type' => 'test', 'name' => 'Node type name', 'description' => 'Node type description', 'title_label' => 'Title label', 'langcode' => 'en'));
$node_type->save();
$job = tmgmt_job_create('en', 'de');
$job->translator = 'test_translator';
$job->save();
$job_item = tmgmt_job_item_create('config', 'node_type', 'node.type.' . $node_type->id(), array('tjid' => $job->id()));
$job_item->save();
$source_plugin = $this->container->get('plugin.manager.tmgmt.source')->createInstance('config');
$data = $source_plugin->getData($job_item);
// Test the name property.
$this->assertEqual($data['name']['#label'], 'Name');
$this->assertEqual($data['name']['#text'], $node_type->label());
$this->assertEqual($data['name']['#translate'], TRUE);
$this->assertEqual($data['description']['#label'], 'Description');
$this->assertEqual($data['description']['#text'], $node_type->getDescription());
$this->assertEqual($data['description']['#translate'], TRUE);
// Test item types.
$this->assertEqual($source_plugin->getItemTypes()['node_type'], t('Content type'));
// Now request a translation and save it back.
$job->requestTranslation();
$items = $job->getItems();
$item = reset($items);
$item->acceptTranslation();
$data = $item->getData();
// Check that the translations were saved correctly.
$language_manager = \Drupal::languageManager();
$language_manager->setConfigOverrideLanguage($language_manager->getLanguage('de'));
$node_type = entity_load('node_type', $node_type->id());
$this->assertEqual($node_type->label(), $data['name']['#translation']['#text']);
$this->assertEqual($node_type->getDescription(), $data['description']['#translation']['#text']);
}
示例15: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('node');
$this->installEntitySchema('comment');
$this->installEntitySchema('taxonomy_term');
CommentType::create(['id' => 'comment_node_page', 'label' => $this->randomMachineName()])->save();
CommentType::create(['id' => 'comment_node_article', 'label' => $this->randomMachineName()])->save();
CommentType::create(['id' => 'comment_node_blog', 'label' => $this->randomMachineName()])->save();
CommentType::create(['id' => 'comment_node_book', 'label' => $this->randomMachineName()])->save();
CommentType::create(['id' => 'comment_node_forum', 'label' => $this->randomMachineName()])->save();
CommentType::create(['id' => 'comment_node_test_content_type', 'label' => $this->randomMachineName()])->save();
NodeType::create(['type' => 'page', 'label' => $this->randomMachineName()])->save();
NodeType::create(['type' => 'article', 'label' => $this->randomMachineName()])->save();
NodeType::create(['type' => 'blog', 'label' => $this->randomMachineName()])->save();
NodeType::create(['type' => 'book', 'label' => $this->randomMachineName()])->save();
NodeType::create(['type' => 'forum', 'label' => $this->randomMachineName()])->save();
NodeType::create(['type' => 'test_content_type', 'label' => $this->randomMachineName()])->save();
Vocabulary::create(['vid' => 'test_vocabulary'])->save();
// Give one unfortunate field instance invalid display settings to ensure
// that the migration provides an empty array as a default (thus avoiding
// an "unsupported operand types" fatal).
Database::getConnection('default', 'migrate')->update('field_config_instance')->fields(array('data' => serialize(array('label' => 'Body', 'widget' => array('type' => 'text_textarea_with_summary', 'settings' => array('rows' => 20, 'summary_rows' => 5), 'weight' => -4, 'module' => 'text'), 'settings' => array('display_summary' => TRUE, 'text_processing' => 1, 'user_register_form' => FALSE), 'display' => array('default' => array('label' => 'hidden', 'type' => 'text_default', 'settings' => array(), 'module' => 'text', 'weight' => 0), 'teaser' => array('label' => 'hidden', 'type' => 'text_summary_or_trimmed', 'settings' => NULL, 'module' => 'text', 'weight' => 0)), 'required' => FALSE, 'description' => ''))))->condition('entity_type', 'node')->condition('bundle', 'article')->condition('field_name', 'body')->execute();
$this->executeMigrations(['d7_field', 'd7_field_instance', 'd7_view_modes', 'd7_field_formatter_settings']);
}