本文整理汇总了PHP中entity_load_multiple_by_properties函数的典型用法代码示例。如果您正苦于以下问题:PHP entity_load_multiple_by_properties函数的具体用法?PHP entity_load_multiple_by_properties怎么用?PHP entity_load_multiple_by_properties使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了entity_load_multiple_by_properties函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testNodeMultipleLoad
/**
* Creates four nodes and ensures that they are loaded correctly.
*/
function testNodeMultipleLoad()
{
$node1 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
$node2 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
$node3 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 0));
$node4 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
// Confirm that promoted nodes appear in the default node listing.
$this->drupalGet('node');
$this->assertText($node1->label(), 'Node title appears on the default listing.');
$this->assertText($node2->label(), 'Node title appears on the default listing.');
$this->assertNoText($node3->label(), 'Node title does not appear in the default listing.');
$this->assertNoText($node4->label(), 'Node title does not appear in the default listing.');
// Load nodes with only a condition. Nodes 3 and 4 will be loaded.
$nodes = entity_load_multiple_by_properties('node', array('promote' => 0));
$this->assertEqual($node3->label(), $nodes[$node3->id()]->label(), 'Node was loaded.');
$this->assertEqual($node4->label(), $nodes[$node4->id()]->label(), 'Node was loaded.');
$count = count($nodes);
$this->assertTrue($count == 2, format_string('@count nodes loaded.', array('@count' => $count)));
// Load nodes by nid. Nodes 1, 2 and 4 will be loaded.
$nodes = Node::loadMultiple(array(1, 2, 4));
$count = count($nodes);
$this->assertTrue(count($nodes) == 3, format_string('@count nodes loaded', array('@count' => $count)));
$this->assertTrue(isset($nodes[$node1->id()]), 'Node is correctly keyed in the array');
$this->assertTrue(isset($nodes[$node2->id()]), 'Node is correctly keyed in the array');
$this->assertTrue(isset($nodes[$node4->id()]), 'Node is correctly keyed in the array');
foreach ($nodes as $node) {
$this->assertTrue(is_object($node), 'Node is an object');
}
}
示例2: testMetatag
/**
* Tests adding and editing values using metatag.
*/
public function testMetatag()
{
// Create a test entity.
$edit = ['name[0][value]' => 'Barfoo', 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')'];
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
$entities = entity_load_multiple_by_properties('entity_test', ['name' => 'Barfoo']);
$this->assertEqual(1, count($entities), 'Entity was saved');
$entity = reset($entities);
// Update the Global defaults and test them.
$values = array('keywords' => 'Purple monkey dishwasher');
$this->drupalPostForm('admin/structure/metatag_defaults/global', $values, 'Save');
$this->assertText('Saved the Global Metatag defaults.');
$this->drupalGet('entity_test/' . $entity->id());
$elements = $this->cssSelect('meta[name=keywords]');
$this->assertTrue(count($elements) === 1, 'Found keywords metatag from defaults');
$this->assertEqual((string) $elements[0]['content'], $values['keywords'], 'Default keywords applied');
// Tests metatags with urls work.
$edit = ['name[0][value]' => 'UrlTags', 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')', 'field_metatag[0][open_graph][og_url]' => 'http://example.com/foo.html'];
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
$entities = entity_load_multiple_by_properties('entity_test', ['name' => 'UrlTags']);
$this->assertEqual(1, count($entities), 'Entity was saved');
$entity = reset($entities);
$this->drupalGet('entity_test/' . $entity->id());
$elements = $this->cssSelect("meta[property='og:url']");
$this->assertTrue(count($elements) === 1, 'Found keywords metatag from defaults');
$this->assertEqual((string) $elements[0]['content'], $edit['field_metatag[0][open_graph][og_url]']);
}
示例3: loadEntityByName
/**
* Loads a test entity by name always resetting the storage cache.
*/
protected function loadEntityByName($entity_type, $name)
{
// Always load the entity from the database to ensure that changes are
// correctly picked up.
$this->container->get('entity.manager')->getStorage($entity_type)->resetCache();
return current(entity_load_multiple_by_properties($entity_type, array('name' => $name)));
}
示例4: at_core_submit_mobile_blocks
/**
* @file
* Save Breadcrumb CSS to file
*/
function at_core_submit_mobile_blocks($values, $theme, $generated_files_path) {
$mobile_blocks_css = array();
$theme_blocks = entity_load_multiple_by_properties('block', ['theme' => $theme]);
if (!empty($theme_blocks)) {
foreach ($theme_blocks as $block_key => $block_values) {
$block_id = $block_values->id();
if (isset($values['settings_mobile_block_show_' . $block_id]) && $values['settings_mobile_block_show_' . $block_id] == 1) {
$block_selector = '#' . Html::getUniqueId('block-' . $block_id);
$mobile_blocks_css[] = $block_selector . ' {display:none}' . "\n";
$mobile_blocks_css[] = '.is-mobile ' . $block_selector . ' {display:block}' . "\n";
}
if (isset($values['settings_mobile_block_hide_' . $block_id]) && $values['settings_mobile_block_hide_' . $block_id] == 1) {
$block_selector = '#' . Html::getUniqueId('block-' . $block_id);
$mobile_blocks_css[] = '.is-mobile ' . $block_selector . ' {display:none}' . "\n";
$mobile_blocks_css[] = $block_selector . ' {display:block}' . "\n";
}
}
}
if (!empty($mobile_blocks_css)) {
$file_name = 'mobile-blocks.css';
$filepath = $generated_files_path . '/' . $file_name;
file_unmanaged_save_data($mobile_blocks_css, $filepath, FILE_EXISTS_REPLACE);
}
}
示例5: testFeedLanguage
/**
* Tests creation of feeds with a language.
*/
public function testFeedLanguage()
{
$admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'administer news feeds', 'access news feeds', 'create article content']);
$this->drupalLogin($admin_user);
// Enable language selection for feeds.
$edit['entity_types[aggregator_feed]'] = TRUE;
$edit['settings[aggregator_feed][aggregator_feed][settings][language][language_alterable]'] = TRUE;
$this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
/** @var \Drupal\aggregator\FeedInterface[] $feeds */
$feeds = array();
// Create feeds.
$feeds[1] = $this->createFeed(NULL, array('langcode[0][value]' => $this->langcodes[1]));
$feeds[2] = $this->createFeed(NULL, array('langcode[0][value]' => $this->langcodes[2]));
// Make sure that the language has been assigned.
$this->assertEqual($feeds[1]->language()->getId(), $this->langcodes[1]);
$this->assertEqual($feeds[2]->language()->getId(), $this->langcodes[2]);
// Create example nodes to create feed items from and then update the feeds.
$this->createSampleNodes();
$this->cronRun();
// Loop over the created feed items and verify that their language matches
// the one from the feed.
foreach ($feeds as $feed) {
/** @var \Drupal\aggregator\ItemInterface[] $items */
$items = entity_load_multiple_by_properties('aggregator_item', array('fid' => $feed->id()));
$this->assertTrue(count($items) > 0, 'Feed items were created.');
foreach ($items as $item) {
$this->assertEqual($item->language()->getId(), $feed->language()->getId());
}
}
}
示例6: testTaxonomyTermMultipleLoad
/**
* Create a vocabulary and some taxonomy terms, ensuring they're loaded
* correctly using entity_load_multiple().
*/
function testTaxonomyTermMultipleLoad()
{
// Create a vocabulary.
$vocabulary = $this->createVocabulary();
// Create five terms in the vocabulary.
$i = 0;
while ($i < 5) {
$i++;
$this->createTerm($vocabulary);
}
// Load the terms from the vocabulary.
$terms = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id()));
$count = count($terms);
$this->assertEqual($count, 5, format_string('Correct number of terms were loaded. @count terms.', array('@count' => $count)));
// Load the same terms again by tid.
$terms2 = Term::loadMultiple(array_keys($terms));
$this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.');
$this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.');
// Remove one term from the array, then delete it.
$deleted = array_shift($terms2);
$deleted->delete();
$deleted_term = Term::load($deleted->id());
$this->assertFalse($deleted_term);
// Load terms from the vocabulary by vid.
$terms3 = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id()));
$this->assertEqual(count($terms3), 4, 'Correct number of terms were loaded.');
$this->assertFalse(isset($terms3[$deleted->id()]));
// Create a single term and load it by name.
$term = $this->createTerm($vocabulary);
$loaded_terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $term->getName()));
$this->assertEqual(count($loaded_terms), 1, 'One term was loaded.');
$loaded_term = reset($loaded_terms);
$this->assertEqual($term->id(), $loaded_term->id(), 'Term loaded by name successfully.');
}
示例7: testTaxonomyImageAccess
public function testTaxonomyImageAccess()
{
$user = $this->drupalCreateUser(array('administer site configuration', 'administer taxonomy', 'access user profiles'));
$this->drupalLogin($user);
// Create a term and upload the image.
$files = $this->drupalGetTestFiles('image');
$image = array_pop($files);
$edit['name[0][value]'] = $this->randomMachineName();
$edit['files[field_test_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
$this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save'));
$terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $edit['name[0][value]']));
$term = reset($terms);
$this->assertText(t('Created new term @name.', array('@name' => $term->getName())));
// Create a user that should have access to the file and one that doesn't.
$access_user = $this->drupalCreateUser(array('access content'));
$no_access_user = $this->drupalCreateUser();
$image = File::load($term->field_test->target_id);
$this->drupalLogin($access_user);
$this->drupalGet(file_create_url($image->getFileUri()));
$this->assertResponse(200, 'Private image on term is accessible with right permission');
$this->drupalLogin($no_access_user);
$this->drupalGet(file_create_url($image->getFileUri()));
$this->assertResponse(403, 'Private image on term not accessible without right permission');
}
示例8: testForumUninstallWithField
/**
* Tests if forum module uninstallation properly deletes the field.
*/
public function testForumUninstallWithField()
{
$this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'administer nodes', 'administer modules', 'delete any forum content', 'administer content types']));
// Ensure that the field exists before uninstallation.
$field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
$this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
// Create a taxonomy term.
$term = entity_create('taxonomy_term', array('name' => t('A term'), 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(), 'description' => '', 'parent' => array(0), 'vid' => 'forums', 'forum_container' => 0));
$term->save();
// Create a forum node.
$node = $this->drupalCreateNode(array('title' => 'A forum post', 'type' => 'forum', 'taxonomy_forums' => array(array('target_id' => $term->id()))));
// Create at least one comment against the forum node.
$comment = entity_create('comment', array('entity_id' => $node->nid->value, 'entity_type' => 'node', 'field_name' => 'comment_forum', 'pid' => 0, 'uid' => 0, 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomMachineName(), 'hostname' => '127.0.0.1'));
$comment->save();
// Attempt to uninstall forum.
$this->drupalGet('admin/modules/uninstall');
// Assert forum is required.
$this->assertNoFieldByName('uninstall[forum]');
$this->drupalGet('admin/modules');
$this->assertText('To uninstall Forum first delete all Forum content');
// Delete the node.
$this->drupalPostForm('node/' . $node->id() . '/delete', array(), t('Delete'));
// Attempt to uninstall forum.
$this->drupalGet('admin/modules/uninstall');
// Assert forum is still required.
$this->assertNoFieldByName('uninstall[forum]');
$this->drupalGet('admin/modules');
$this->assertText('To uninstall Forum first delete all Forums terms');
// Delete any forum terms.
$vid = $this->config('forum.settings')->get('vocabulary');
$terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]);
foreach ($terms as $term) {
$term->delete();
}
// Ensure that the forum node type can not be deleted.
$this->drupalGet('admin/structure/types/manage/forum');
$this->assertNoLink(t('Delete'));
// Now attempt to uninstall forum.
$this->drupalGet('admin/modules/uninstall');
// Assert forum is no longer required.
$this->assertFieldByName('uninstall[forum]');
$this->drupalGet('admin/modules');
$this->assertNoText('To uninstall Forum first delete all Forum content');
$this->drupalPostForm('admin/modules/uninstall', array('uninstall[forum]' => 1), t('Uninstall'));
$this->drupalPostForm(NULL, [], t('Uninstall'));
// Check that the field is now deleted.
$field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
$this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
// Check that a node type with a machine name of forum can be created after
// uninstalling the forum module and the node type is not locked.
$edit = array('name' => 'Forum', 'title_label' => 'title for forum', 'type' => 'forum');
$this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type'));
$this->assertTrue((bool) NodeType::load('forum'), 'Node type with machine forum created.');
$this->drupalGet('admin/structure/types/manage/forum');
$this->clickLink(t('Delete'));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertResponse(200);
$this->assertFalse((bool) NodeType::load('forum'), 'Node type with machine forum deleted.');
}
示例9: purgeFieldData
/**
* Purges a field data.
*/
public static function purgeFieldData()
{
do {
field_purge_batch(1000);
$properties = array('deleted' => TRUE, 'include_deleted' => TRUE);
$fields = entity_load_multiple_by_properties('field_config', $properties);
} while ($fields);
}
示例10: testDelete
/**
* Test deleting functionality.
*/
public function testDelete()
{
$feed = $this->createFeed();
$this->updateAndDelete($feed, NULL);
// Make sure the feed title is changed.
$entities = entity_load_multiple_by_properties('aggregator_feed', array('description' => $feed->description->value));
$this->assertTrue(empty($entities));
}
示例11: drupalGetBlockByInfo
/**
* Get a custom block from the database based on its title.
*
* @param $info
* A block title, usually generated by $this->randomMachineName().
* @param $reset
* (optional) Whether to reset the entity cache.
*
* @return \Drupal\block\BlockInterface
* A block entity matching $info.
*/
function drupalGetBlockByInfo($info, $reset = FALSE)
{
if ($reset) {
\Drupal::entityManager()->getStorage('block_content')->resetCache();
}
$blocks = entity_load_multiple_by_properties('block_content', array('info' => $info));
// Get the first block returned from the database.
$returned_block = reset($blocks);
return $returned_block;
}
示例12: testTermDelete
/**
* Deleting terms should also remove related vocabulary.
* Deleting an invalid term should silently fail.
*/
public function testTermDelete()
{
$vocabulary = $this->createVocabulary();
$valid_term = $this->createTerm($vocabulary);
// Delete a valid term.
$valid_term->delete();
$terms = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id()));
$this->assertTrue(empty($terms), 'Vocabulary is empty after deletion');
// Delete an invalid term. Should not throw any notices.
entity_delete_multiple('taxonomy_term', array(42));
}
示例13: generateFields
/**
* Enrich the $object that is about to be saved with arbitrary
* information in each of its fields.
*/
public static function generateFields(EntityInterface &$object, $entity_type, $bundle_name, $form_mode = 'default', $namespace = 'devel_generate')
{
$instances = entity_load_multiple_by_properties('field_instance_config', array('entity_type' => $entity_type, 'bundle' => $bundle_name));
$field_types = \Drupal::service('plugin.manager.field.field_type')->getDefinitions();
$skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields'];
foreach (explode(',', $skips) as $skip) {
unset($instances[$skip]);
}
foreach ($instances as $instance) {
$field = $instance->getFieldStorageDefinition();
$cardinality = $field->getCardinality();
$field_name = $field->getName();
$object_field = array();
// If module handles own multiples, then only call its hook once.
$form_display_options = entity_get_form_display($entity_type, $bundle_name, $form_mode)->getComponent($field_name);
$plugin_definition = \Drupal::service('plugin.manager.field.widget')->getDefinition($form_display_options['type']);
if (isset($plugin_definition['multiple_values']) && $plugin_definition['multiple_values'] === TRUE) {
$max = 0;
} else {
switch ($cardinality) {
case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
$max = rand(0, 3);
//just an arbitrary number for 'unlimited'
break;
default:
$max = $cardinality - 1;
break;
}
}
for ($i = 0; $i <= $max; $i++) {
$provider = $field_types[$field->type]['provider'];
if (!in_array($provider, array('file', 'image', 'taxonomy', 'number', 'text', 'options', 'email', 'link'))) {
continue;
}
$devel_generate_field_factory = new DevelGenerateFieldFactory();
$devel_generate_field_object = $devel_generate_field_factory->createInstance($provider, $namespace);
if ($devel_generate_field_object instanceof DevelGenerateFieldBaseInterface) {
if ($result = $devel_generate_field_object->generate($object, $instance, $plugin_definition, $form_display_options)) {
if (isset($plugin_definition['multiple_values']) && $plugin_definition['multiple_values'] === TRUE) {
// Fields that handle their own multiples will add their own deltas.
$object_field = $result;
} else {
// When multiples are handled by the content module, add a delta for each result.
$object_field[$i] = $result;
}
}
}
}
$object->{$field_name} = $object_field;
}
}
示例14: testMenuLanguage
/**
* Tests menu language settings and the defaults for menu link items.
*/
function testMenuLanguage()
{
// Create a test menu to test the various language-related settings.
// Machine name has to be lowercase.
$menu_name = Unicode::strtolower($this->randomMachineName(16));
$label = $this->randomString();
$edit = array('id' => $menu_name, 'description' => '', 'label' => $label, 'langcode' => 'aa');
$this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'bb', 'language_show' => TRUE));
// Check menu language.
$this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.');
// Test menu link language.
$link_path = '<front>';
// Add a menu link.
$link_title = $this->randomString();
$edit = array('title[0][value]' => $link_title, 'url' => $link_path);
$this->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save'));
// Check the link was added with the correct menu link default language.
$menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title));
$menu_link = reset($menu_links);
$this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'bb'));
// Edit menu link default, changing it to cc.
language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'cc', 'language_show' => TRUE));
// Add a menu link.
$link_title = $this->randomString();
$edit = array('title[0][value]' => $link_title, 'url' => $link_path);
$this->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save'));
// Check the link was added with the correct new menu link default language.
$menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title));
$menu_link = reset($menu_links);
$this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'cc'));
// Now change the language of the new link to 'bb'.
$edit = array('langcode' => 'bb');
$this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save'));
$this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'bb'));
// Saving menu link items ends up on the edit menu page. To check the menu
// link has the correct language default on edit, go to the menu link edit
// page first.
$this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit');
// Check that the language selector has the correct default value.
$this->assertOptionSelected('edit-langcode', 'bb', 'The menu link language was correctly selected.');
// Edit menu to hide the language select on menu link item add.
language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'cc', 'language_show' => FALSE));
// Check that the language selector is not available on menu link add page.
$this->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
$this->assertNoField('edit-langcode', 'The language selector field was hidden the page');
}
示例15: populateFields
/**
* Populate the fields on a given entity with sample values.
*
* @param $entity
* The entity to be enriched with sample field values.
*/
public function populateFields(EntityInterface $entity)
{
$instances = entity_load_multiple_by_properties('field_instance_config', array('entity_type' => $entity->getEntityType()->id(), 'bundle' => $entity->bundle()));
if ($skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields']) {
foreach (explode(',', $skips) as $skip) {
unset($instances[$skip]);
}
}
foreach ($instances as $instance) {
/** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
$field_storage = $instance->getFieldStorageDefinition();
$max = $cardinality = $field_storage->getCardinality();
if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
// Just an arbitrary number for 'unlimited'
$max = rand(1, 3);
}
$field_name = $field_storage->getName();
$entity->{$field_name}->generateSampleItems($max);
}
}