本文整理汇总了PHP中Drupal::entityTypeManager方法的典型用法代码示例。如果您正苦于以下问题:PHP Drupal::entityTypeManager方法的具体用法?PHP Drupal::entityTypeManager怎么用?PHP Drupal::entityTypeManager使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal
的用法示例。
在下文中一共展示了Drupal::entityTypeManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testRoleEntityClone
public function testRoleEntityClone() {
$edit = [
'label' => 'Test role for clone',
'id' => 'test_role_for_clone',
];
$this->drupalPostForm("/admin/people/roles/add", $edit, t('Save'));
$roles = \Drupal::entityTypeManager()
->getStorage('user_role')
->loadByProperties([
'id' => $edit['id'],
]);
$role = reset($roles);
$edit = [
'id' => 'test_role_cloned',
'label' => 'Test role cloned',
];
$this->drupalPostForm('entity_clone/user_role/' . $role->id(), $edit, t('Clone'));
$roles = \Drupal::entityTypeManager()
->getStorage('user_role')
->loadByProperties([
'id' => $edit['id'],
]);
$role = reset($roles);
$this->assertTrue($role, 'Test role cloned found in database.');
}
示例2: testDateFormatEntityClone
public function testDateFormatEntityClone() {
$edit = [
'label' => 'Test date format for clone',
'id' => 'test_date_format_for_clone',
'date_format_pattern' => 'Y m d',
];
$this->drupalPostForm("admin/config/regional/date-time/formats/add", $edit, t('Add format'));
$date_formats = \Drupal::entityTypeManager()
->getStorage('date_format')
->loadByProperties([
'id' => $edit['id'],
]);
$date_format = reset($date_formats);
$edit = [
'id' => 'test_date_format_cloned',
'label' => 'Test date format cloned',
];
$this->drupalPostForm('entity_clone/date_format/' . $date_format->id(), $edit, t('Clone'));
$date_formats = \Drupal::entityTypeManager()
->getStorage('date_format')
->loadByProperties([
'id' => $edit['id'],
]);
$date_format = reset($date_formats);
$this->assertTrue($date_format, 'Test date format cloned found in database.');
}
示例3: getParentEntity
/**
* {@inheritdoc}
*/
public function getParentEntity()
{
if (!isset($this->get('parent_type')->value) || !isset($this->get('parent_id')->value)) {
return NULL;
}
return \Drupal::entityTypeManager()->getStorage($this->get('parent_type')->value)->load($this->get('parent_id')->value);
}
示例4: testEnablingOnExistingContent
/**
* Tests enabling moderation on an existing node-type, with content.
*/
public function testEnablingOnExistingContent()
{
// Create a node type that is not moderated.
$this->drupalLogin($this->adminUser);
$this->createContentTypeFromUi('Not moderated', 'not_moderated');
$this->grantUserPermissionToCreateContentOfType($this->adminUser, 'not_moderated');
// Create content.
$this->drupalGet('node/add/not_moderated');
$this->drupalPostForm(NULL, ['title[0][value]' => 'Test'], t('Save and publish'));
$this->assertText('Not moderated Test has been created.');
// Now enable moderation state.
$this->enableModerationThroughUi('not_moderated', ['draft', 'needs_review', 'published'], 'draft');
// And make sure it works.
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['title' => 'Test']);
if (empty($nodes)) {
$this->fail('Could not load node with title Test');
return;
}
$node = reset($nodes);
$this->drupalGet('node/' . $node->id());
$this->assertResponse(200);
$this->assertLinkByHref('node/' . $node->id() . '/edit');
$this->drupalGet('node/' . $node->id() . '/edit');
$this->assertResponse(200);
$this->assertRaw('Save and Create New Draft');
$this->assertNoRaw('Save and publish');
}
示例5: testEnablingOnExistingContent
/**
* A node type without moderation state enabled.
*/
public function testEnablingOnExistingContent()
{
$this->drupalLogin($this->adminUser);
$this->createContentTypeFromUI('Not moderated', 'not_moderated');
$this->grantUserPermissionToCreateContentOfType($this->adminUser, 'not_moderated');
$this->drupalGet('node/add/not_moderated');
$this->drupalPostForm(NULL, ['title[0][value]' => 'Test'], t('Save and publish'));
$this->assertText('Not moderated Test has been created.');
// Now enable moderation state.
$this->drupalGet('admin/structure/types/manage/not_moderated/moderation');
$this->drupalPostForm(NULL, ['enable_moderation_state' => 1, 'allowed_moderation_states[draft]' => 1, 'allowed_moderation_states[needs_review]' => 1, 'allowed_moderation_states[published]' => 1, 'default_moderation_state' => 'draft'], t('Save'));
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['title' => 'Test']);
if (empty($nodes)) {
$this->fail('Could not load node with title Test');
return;
}
$node = reset($nodes);
$this->drupalGet('node/' . $node->id());
$this->assertResponse(200);
$this->assertLinkByHref('node/' . $node->id() . '/edit');
$this->drupalGet('node/' . $node->id() . '/edit');
$this->assertResponse(200);
$this->assertRaw('Save and Create New Draft');
$this->assertNoRaw('Save and publish');
}
示例6: testSearchPageEntityClone
public function testSearchPageEntityClone() {
$edit = [
'label' => 'Test search page for clone',
'id' => 'test_search_page_for_clone',
'path' => 'test_search_page_for_clone_url',
];
$this->drupalPostForm("/admin/config/search/pages/add/node_search", $edit, t('Add search page'));
$search_pages = \Drupal::entityTypeManager()
->getStorage('search_page')
->loadByProperties([
'id' => $edit['id'],
]);
$search_page = reset($search_pages);
$edit = [
'id' => 'test_search_page_cloned',
'label' => 'Test search page cloned',
];
$this->drupalPostForm('entity_clone/search_page/' . $search_page->id(), $edit, t('Clone'));
$search_pages = \Drupal::entityTypeManager()
->getStorage('search_page')
->loadByProperties([
'id' => $edit['id'],
]);
$search_page = reset($search_pages);
$this->assertTrue($search_page, 'Test search page cloned found in database.');
}
示例7: buildRolesProfilesTable
/**
* Returns roles-profiles table.
*/
public function buildRolesProfilesTable(array $roles_profiles)
{
$rp_table = array('#type' => 'table');
// Prepare roles. Reverse the role order to prioritize the permissive ones.
$roles = array_reverse(user_roles());
$wrappers = \Drupal::service('stream_wrapper_manager')->getNames(StreamWrapperInterface::WRITE_VISIBLE);
// Prepare profile options
$options = array('' => '-' . $this->t('None') . '-');
foreach (\Drupal::entityTypeManager()->getStorage('imce_profile')->loadMultiple() as $pid => $profile) {
$options[$pid] = $profile->label();
}
// Build header
$imce_url = \Drupal::url('imce.page');
$rp_table['#header'] = array($this->t('Role'));
$default = file_default_scheme();
foreach ($wrappers as $scheme => $name) {
$url = $scheme === $default ? $imce_url : $imce_url . '/' . $scheme;
$rp_table['#header'][]['data'] = array('#markup' => '<a href="' . $url . '">' . Html::escape($name) . '</a>');
}
// Build rows
foreach ($roles as $rid => $role) {
$rp_table[$rid]['role_name'] = array('#plain_text' => $role->label());
foreach ($wrappers as $scheme => $name) {
$rp_table[$rid][$scheme] = array('#type' => 'select', '#options' => $options, '#default_value' => isset($roles_profiles[$rid][$scheme]) ? $roles_profiles[$rid][$scheme] : '');
}
}
// Add description
$rp_table['#prefix'] = '<h3>' . $this->t('Role-profile assignments') . '</h3>';
$rp_table['#suffix'] = '<div class="description">' . $this->t('Assign configuration profiles to user roles for available file systems. The default file system %name is accessible at :url path.', array('%name' => $wrappers[file_default_scheme()], ':url' => $imce_url)) . '</div>';
return $rp_table;
}
示例8: completeSale
/**
* {@inheritdoc}
*/
public function completeSale($order, $login = FALSE)
{
// Empty that cart...
$this->emptyCart();
// Force the order to load from the DB instead of the entity cache.
// @todo Remove this once uc_payment_enter() can modify order objects?
// @todo Should we be overwriting $order with this newly-loaded db_order?
$db_order = \Drupal::entityTypeManager()->getStorage('uc_order')->loadUnchanged($order->id());
$order->data = $db_order->data;
// Ensure that user creation and triggers are only run once.
if (empty($order->data->complete_sale)) {
$this->completeSaleAccount($order);
// Move an order's status from "In checkout" to "Pending".
if ($order->getStateId() == 'in_checkout') {
$order->setStatusId(uc_order_state_default('post_checkout'));
}
$order->save();
// Invoke the checkout complete trigger and hook.
$account = $order->getOwner();
\Drupal::moduleHandler()->invokeAll('uc_checkout_complete', array($order, $account));
// rules_invoke_event('uc_checkout_complete', $order);
}
$type = $order->data->complete_sale;
// Log in new users, if requested.
if ($type == 'new_user' && $login && $this->currentUser->isAnonymous()) {
$type = 'new_user_logged_in';
user_login_finalize($order->getOwner());
}
$message = \Drupal::config('uc_cart.messages')->get($type);
$message = \Drupal::token()->replace($message, array('uc_order' => $order));
$variables['!new_username'] = isset($order->data->new_user_name) ? $order->data->new_user_name : '';
$variables['!new_password'] = isset($order->password) ? $order->password : t('Your password');
$message = strtr($message, $variables);
return array('#theme' => 'uc_cart_complete_sale', '#message' => array('#markup' => $message), '#order' => $order);
}
示例9: testForm
/**
* Tests the add page.
*/
public function testForm()
{
$entities = [];
$selection = [];
for ($i = 0; $i < 2; $i++) {
$entity = EnhancedEntity::create(['type' => 'default']);
$entity->save();
$entities[$entity->id()] = $entity;
$langcode = $entity->language()->getId();
$selection[$entity->id()][$langcode] = $langcode;
}
// Add the selection to the tempstore just like DeleteAction would.
$tempstore = \Drupal::service('user.private_tempstore')->get('entity_delete_multiple_confirm');
$tempstore->set($this->account->id(), $selection);
$this->drupalGet('/entity_test_enhanced/delete');
$assert = $this->assertSession();
$assert->statusCodeEquals(200);
$assert->elementTextContains('css', '.page-title', 'Are you sure you want to delete these items?');
$delete_button = $this->getSession()->getPage()->findButton('Delete');
$delete_button->click();
$assert = $this->assertSession();
$assert->addressEquals('/entity_test_enhanced');
$assert->responseContains('Deleted 2 items.');
\Drupal::entityTypeManager()->getStorage('entity_test_enhanced')->resetCache();
$remaining_entities = EnhancedEntity::loadMultiple(array_keys($selection));
$this->assertEmpty($remaining_entities);
}
示例10: testTaxonomyTerms
/**
* Tests the Drupal 6 taxonomy term to Drupal 8 migration.
*/
public function testTaxonomyTerms()
{
$expected_results = array('1' => array('source_vid' => 1, 'vid' => 'vocabulary_1_i_0_', 'weight' => 0, 'parent' => array(0)), '2' => array('source_vid' => 2, 'vid' => 'vocabulary_2_i_1_', 'weight' => 3, 'parent' => array(0)), '3' => array('source_vid' => 2, 'vid' => 'vocabulary_2_i_1_', 'weight' => 4, 'parent' => array(2)), '4' => array('source_vid' => 3, 'vid' => 'vocabulary_3_i_2_', 'weight' => 6, 'parent' => array(0)), '5' => array('source_vid' => 3, 'vid' => 'vocabulary_3_i_2_', 'weight' => 7, 'parent' => array(4)), '6' => array('source_vid' => 3, 'vid' => 'vocabulary_3_i_2_', 'weight' => 8, 'parent' => array(4, 5)));
$terms = Term::loadMultiple(array_keys($expected_results));
// Find each term in the tree.
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$vids = array_unique(array_column($expected_results, 'vid'));
$tree_terms = [];
foreach ($vids as $vid) {
foreach ($storage->loadTree($vid) as $term) {
$tree_terms[$term->tid] = $term;
}
}
foreach ($expected_results as $tid => $values) {
/** @var Term $term */
$term = $terms[$tid];
$this->assertIdentical("term {$tid} of vocabulary {$values['source_vid']}", $term->name->value);
$this->assertIdentical("description of term {$tid} of vocabulary {$values['source_vid']}", $term->description->value);
$this->assertIdentical($values['vid'], $term->vid->target_id);
$this->assertIdentical((string) $values['weight'], $term->weight->value);
if ($values['parent'] === array(0)) {
$this->assertNull($term->parent->target_id);
} else {
$parents = array();
foreach (\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid) as $parent) {
$parents[] = (int) $parent->id();
}
$this->assertIdentical($parents, $values['parent']);
}
$this->assertArrayHasKey($tid, $tree_terms, "Term {$tid} exists in vocabulary tree");
$tree_term = $tree_terms[$tid];
$this->assertEquals($values['parent'], $tree_term->parents, "Term {$tid} has correct parents in vocabulary tree");
}
}
示例11: testFilterFormatEntityClone
public function testFilterFormatEntityClone() {
$edit = [
'name' => 'Test filter format for clone',
'format' => 'test_filter_format_for_clone',
];
$this->drupalPostForm("admin/config/content/formats/add", $edit, t('Save configuration'));
$filter_formats = \Drupal::entityTypeManager()
->getStorage('filter_format')
->loadByProperties([
'format' => $edit['format'],
]);
$filter_format = reset($filter_formats);
$edit = [
'id' => 'test_filter_format_cloned',
'label' => 'Test filter format cloned',
];
$this->drupalPostForm('entity_clone/filter_format/' . $filter_format->id(), $edit, t('Clone'));
$filter_formats = \Drupal::entityTypeManager()
->getStorage('filter_format')
->loadByProperties([
'format' => $edit['id'],
]);
$filter_format = reset($filter_formats);
$this->assertTrue($filter_format, 'Test filter format cloned found in database.');
}
示例12: testLanguageEntityClone
public function testLanguageEntityClone() {
$edit = [
'predefined_langcode' => 'fr'
];
$this->drupalPostForm("/admin/config/regional/language/add", $edit, t('Add language'));
$languages = \Drupal::entityTypeManager()
->getStorage('configurable_language')
->loadByProperties([
'id' => 'fr',
]);
$language = reset($languages);
$edit = [
'id' => 'test_language_cloned',
'label' => 'French language cloned',
];
$this->drupalPostForm('entity_clone/configurable_language/' . $language->id(), $edit, t('Clone'));
$languages = \Drupal::entityTypeManager()
->getStorage('configurable_language')
->loadByProperties([
'id' => $edit['id'],
]);
$language = reset($languages);
$this->assertTrue($language, 'Test language cloned found in database.');
}
示例13: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
$referenced_entities = $items->referencedEntities();
$element = parent::formElement($items, $delta, $element, $form, $form_state);
// If this is an existing (not new item).
if ($delta < count($referenced_entities)) {
// Mark element as being existing, not new item.
// Top level of the returned element must be called 'target_id',
// so we cannot create a container.
// Autocomplete element does some fancy processing to handle empty strings,
// so we must use an autocomplete element not a hidden or textfield element.
// But #states[#visible] does not seem to have an option to always hide.,
// and autocomplete elements don't seem to accept #attributes, so we must
// use #prefix and #suffix to add a class so that we can hide it.
$element['#prefix'] = '<div class="er-enhanced-existing">';
$element['#suffix'] = '</div>';
if ($this->getSetting('preview')) {
// Add preview.
$element['#prefix'] = '<div class="er-enhanced-existing er-enhanced-previewing">';
$element['#attached']['library'][] = 'ahs_er_enhanced/preview';
$entityTypeName = $referenced_entities[$delta]->getEntityType()->id();
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entityTypeName);
$preview = $view_builder->view($referenced_entities[$delta], $this->getSetting('preview_view_mode'));
$element['preview_container'] = ['#type' => 'container', '#attributes' => ['class' => ['er-enhanced-preview']], 'preview' => $preview];
// Add a remove link to the preview.
$element['remove'] = ['#markup' => '<a class="er-enhanced-remove" href="">' . t('Remove') . '</a>'];
$element['#attached']['library'][] = 'ahs_er_enhanced/remove';
}
} else {
$element['#prefix'] = '<div class="er-enhanced-new">';
$element['#suffix'] = '</div>';
}
return $element;
}
示例14: testUserRegisterForm
/**
* Test user registration integration.
*/
public function testUserRegisterForm()
{
$id = $this->type->id();
$field_name = $this->field->getName();
$this->field->setRequired(TRUE);
$this->field->save();
// Allow registration without administrative approval and log in user
// directly after registering.
\Drupal::configFactory()->getEditable('user.settings')->set('register', USER_REGISTER_VISITORS)->set('verify_mail', 0)->save();
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['view own test profile']);
// Verify that the additional profile field is attached and required.
$name = $this->randomMachineName();
$pass_raw = $this->randomMachineName();
$edit = ['name' => $name, 'mail' => $this->randomMachineName() . '@example.com', 'pass[pass1]' => $pass_raw, 'pass[pass2]' => $pass_raw];
$this->drupalPostForm('user/register', $edit, t('Create new account'));
$this->assertRaw(new FormattableMarkup('@name field is required.', ['@name' => $this->field->getLabel()]));
// Verify that we can register.
$edit["entity_" . $id . "[{$field_name}][0][value]"] = $this->randomMachineName();
$this->drupalPostForm(NULL, $edit, t('Create new account'));
$this->assertText(new FormattableMarkup('Registration successful. You are now logged in.', []));
$new_user = user_load_by_name($name);
$this->assertTrue($new_user->isActive(), 'New account is active after registration.');
// Verify that a new profile was created for the new user ID.
$profile = \Drupal::entityTypeManager()->getStorage('profile')->loadByUser($new_user, $this->type->id());
$this->assertEqual($profile->get($field_name)->value, $edit["entity_" . $id . "[{$field_name}][0][value]"], 'Field value found in loaded profile.');
// Verify that the profile field value appears on the user account page.
$this->drupalGet('user');
$this->assertText($edit["entity_" . $id . "[{$field_name}][0][value]"], 'Field value found on user account page.');
}
示例15: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installConfig(['system']);
$this->installConfig(['field']);
$this->installConfig(['text']);
$this->installConfig(['address']);
$this->installEntitySchema('entity_test');
ConfigurableLanguage::createFromLangcode('zh-hant')->save();
// The address module is never installed, so the importer doesn't run
// automatically. Instead, we manually import the address formats we need.
$country_codes = ['AD', 'SV', 'TW', 'US', 'ZZ'];
$importer = \Drupal::service('address.address_format_importer');
$importer->importEntities($country_codes);
$importer->importTranslations(['zh-hant']);
$this->entityType = 'entity_test';
$this->bundle = $this->entityType;
$this->fieldName = Unicode::strtolower($this->randomMachineName());
$field_storage = FieldStorageConfig::create(['field_name' => $this->fieldName, 'entity_type' => $this->entityType, 'type' => 'address']);
$field_storage->save();
$field = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $this->bundle, 'label' => $this->randomMachineName()]);
$field->save();
$values = ['targetEntityType' => $this->entityType, 'bundle' => $this->bundle, 'mode' => 'default', 'status' => TRUE];
$this->display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->create($values);
$this->display->setComponent($this->fieldName, ['type' => 'address_default', 'settings' => []]);
$this->display->save();
}