本文整理汇总了PHP中Drupal\block\Entity\Block::load方法的典型用法代码示例。如果您正苦于以下问题:PHP Block::load方法的具体用法?PHP Block::load怎么用?PHP Block::load使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\block\Entity\Block
的用法示例。
在下文中一共展示了Block::load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: index
public function index($count)
{
// Esta forma de llevar a cabo conexiones es mas rapida sin usar dependency injection
// sin embargo es considerada mala practica
// $connection = \Drupal::database();
$select = $this->db->select('node', 'n');
$select->fields('n');
$select->range(0, $count);
$result = $select->execute()->fetchAll();
$query = $this->entityQuery->get('node');
$query->condition('status', 1);
$query->range(0, $count);
$result = $query->execute();
$storage = $this->entityManager->getStorage('node');
$render_controller = $this->entityManager->getViewBuilder('node');
$nodes = $storage->loadMultiple($result);
$some_nodes = array();
foreach ($nodes as $nid => $node) {
$some_nodes[$nid] = $node->getTitle();
}
$dispatcher = $this->eventDisp;
$e = new NodeFancyDisplayEvent($some_nodes);
$event = $dispatcher->dispatch('rootstack_sandbox.fancy_node_edit', $e);
$some_nodes = $event->getFancyNodes();
// Generar links en D8..... http://drupal.stackexchange.com/questions/144992/how-do-you-create-a-link-in-drupal-8 :(
$block = \Drupal\block\Entity\Block::load('views_block__sdf_block_1');
$test = $this->entityManager->getViewBuilder('block')->view($block);
return ['#theme' => 'fancy_nodes', '#summary' => $render_controller->view(array_values($nodes)[0]), '#some_nodes' => $some_nodes, '#total' => count($nodes)];
}
示例2: testBasicRendering
/**
* Tests the rendering of blocks.
*/
public function testBasicRendering()
{
\Drupal::state()->set('block_test.content', '');
$entity = $this->controller->create(array('id' => 'test_block1', 'theme' => 'stark', 'plugin' => 'test_html'));
$entity->save();
// Test the rendering of a block.
$entity = Block::load('test_block1');
$output = entity_view($entity, 'block');
$expected = array();
$expected[] = '<div id="block-test-block1">';
$expected[] = ' ';
$expected[] = ' ';
$expected[] = ' ';
$expected[] = ' </div>';
$expected[] = '';
$expected_output = implode("\n", $expected);
$this->assertEqual($this->renderer->renderRoot($output), $expected_output);
// Reset the HTML IDs so that the next render is not affected.
Html::resetSeenIds();
// Test the rendering of a block with a given title.
$entity = $this->controller->create(array('id' => 'test_block2', 'theme' => 'stark', 'plugin' => 'test_html', 'settings' => array('label' => 'Powered by Bananas')));
$entity->save();
$output = entity_view($entity, 'block');
$expected = array();
$expected[] = '<div id="block-test-block2">';
$expected[] = ' ';
$expected[] = ' <h2>Powered by Bananas</h2>';
$expected[] = ' ';
$expected[] = ' ';
$expected[] = ' </div>';
$expected[] = '';
$expected_output = implode("\n", $expected);
$this->assertEqual($this->renderer->renderRoot($output), $expected_output);
}
示例3: testPrintableBlock
/**
* Tests the functionality of the Printable block.
*/
public function testPrintableBlock()
{
$this->drupalLogin($this->adminUser);
$edit = ['id' => strtolower($this->randomMachineName()), 'settings[label]' => $this->randomMachineName(8), 'region' => 'sidebar_first', 'visibility[node_type][bundles][article]' => 'article'];
$theme = \Drupal::service('theme_handler')->getDefault();
$this->drupalPostForm("admin/structure/block/add/printable_links_block%3Anode/{$theme}", $edit, t('Save block'));
$block = Block::load($edit['id']);
$visibility = $block->getVisibility();
$this->assertTrue(isset($visibility['node_type']['bundles']['article']), 'Visibility settings were saved to configuration');
// Test deleting the block from the edit form.
$this->drupalGet('admin/structure/block/manage/' . $edit['id']);
$this->clickLink(t('Delete'));
$this->assertRaw(t('Are you sure you want to delete the block %name?', array('%name' => $edit['settings[label]'])));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertRaw(t('The block %name has been deleted.', array('%name' => $edit['settings[label]'])));
}
示例4: testUpdateHookN
/**
* Tests that block context mapping is updated properly.
*/
public function testUpdateHookN()
{
$this->runUpdates();
$this->assertRaw('Encountered an unknown context mapping key coming probably from a contributed or custom module: One or more mappings could not be updated. Please manually review your visibility settings for the following blocks, which are disabled now:<ul><li>User login (Visibility: Baloney spam)</li></ul>');
// Disable maintenance mode.
\Drupal::state()->set('system.maintenance_mode', FALSE);
// We finished updating so we can login the user now.
$this->drupalLogin($this->rootUser);
// The block that we are testing has the following visibility rules:
// - only visible on node pages
// - only visible to authenticated users.
$block_title = 'Test for 2354889';
// Create two nodes, a page and an article.
$page = Node::create(['type' => 'page', 'title' => 'Page node']);
$page->save();
$article = Node::create(['type' => 'article', 'title' => 'Article node']);
$article->save();
// Check that the block appears only on Page nodes for authenticated users.
$this->drupalGet('node/' . $page->id());
$this->assertRaw($block_title, 'Test block is visible on a Page node as an authenticated user.');
$this->drupalGet('node/' . $article->id());
$this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an authenticated user.');
$this->drupalLogout();
// Check that the block does not appear on any page for anonymous users.
$this->drupalGet('node/' . $page->id());
$this->assertNoRaw($block_title, 'Test block is not visible on a Page node as an anonymous user.');
$this->drupalGet('node/' . $article->id());
$this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an anonymous user.');
// Ensure that all the context mappings got updated properly.
$block = Block::load('testfor2354889');
$visibility = $block->get('visibility');
$this->assertEqual('@node.node_route_context:node', $visibility['node_type']['context_mapping']['node']);
$this->assertEqual('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
$this->assertEqual('@language.current_language_context:language_interface', $visibility['language']['context_mapping']['language']);
// Check that a block with invalid context is being disabled and that it can
// still be edited afterward.
$disabled_block = Block::load('thirdtestfor2354889');
$this->assertFalse($disabled_block->status(), 'Block with invalid context is disabled');
$this->assertEqual(['thirdtestfor2354889' => ['missing_context_ids' => ['baloney.spam' => ['node_type']], 'status' => TRUE]], \Drupal::keyValue('update_backup')->get('block_update_8001'));
$disabled_block_visibility = $disabled_block->get('visibility');
$this->assertTrue(!isset($disabled_block_visibility['node_type']), 'The problematic visibility condition has been removed.');
$admin_user = $this->drupalCreateUser(['administer blocks']);
$this->drupalLogin($admin_user);
$this->drupalGet('admin/structure/block/manage/thirdtestfor2354889');
$this->assertResponse('200');
}
示例5: testLanguageBlockVisibilityLanguageDelete
/**
* Tests if the visibility settings are removed if the language is deleted.
*/
public function testLanguageBlockVisibilityLanguageDelete()
{
// Enable a standard block and set the visibility setting for one language.
$edit = array('visibility' => array('language' => array('language_type' => 'language_interface', 'langcodes' => array('fr' => 'fr'))));
$block = $this->drupalPlaceBlock('system_powered_by_block', $edit);
// Check that we have the language in config after saving the setting.
$visibility = $block->getVisibility();
$language = $visibility['language']['langcodes']['fr'];
$this->assertTrue('fr' === $language, 'Language is set in the block configuration.');
// Delete the language.
$this->drupalPostForm('admin/config/regional/language/delete/fr', array(), t('Delete'));
// Check that the language is no longer stored in the configuration after
// it is deleted.
$block = Block::load($block->id());
$visibility = $block->getVisibility();
$this->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.');
}
示例6: assertEntity
/**
* Asserts various aspects of a block.
*
* @param string $id
* The block ID.
* @param string $plugin_id
* The block's plugin ID.
* @param array $roles
* Role IDs the block is expected to have.
* @param string $pages
* The list of pages on which the block should appear.
* @param string $region
* The display region.
* @param string $theme
* The theme.
* @param string $weight
* The block weight.
*/
public function assertEntity($id, $plugin_id, array $roles, $pages, $region, $theme, $weight)
{
$block = Block::load($id);
$this->assertTrue($block instanceof Block);
/** @var \Drupal\block\BlockInterface $block */
$this->assertIdentical($plugin_id, $block->getPluginId());
$visibility = $block->getVisibility();
if ($roles) {
$this->assertIdentical($roles, array_values($visibility['user_role']['roles']));
$this->assertIdentical('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
}
if ($pages) {
$this->assertIdentical($pages, $visibility['request_path']['pages']);
}
$this->assertIdentical($region, $block->getRegion());
$this->assertIdentical($theme, $block->getTheme());
$this->assertIdentical($weight, $block->getWeight());
}
示例7: testBlockHooks
/**
* Tests hook invocations for CRUD operations on blocks.
*/
public function testBlockHooks()
{
$entity = entity_create('block', array('id' => 'stark_test_html', 'plugin' => 'test_html', 'theme' => 'stark'));
$this->assertHookMessageOrder(array('entity_crud_hook_test_block_create called', 'entity_crud_hook_test_entity_create called for type block'));
$GLOBALS['entity_crud_hook_test'] = array();
$entity->save();
$this->assertHookMessageOrder(array('entity_crud_hook_test_block_presave called', 'entity_crud_hook_test_entity_presave called for type block', 'entity_crud_hook_test_block_insert called', 'entity_crud_hook_test_entity_insert called for type block'));
$GLOBALS['entity_crud_hook_test'] = array();
$entity = Block::load($entity->id());
$this->assertHookMessageOrder(array('entity_crud_hook_test_entity_load called for type block', 'entity_crud_hook_test_block_load called'));
$GLOBALS['entity_crud_hook_test'] = array();
$entity->label = 'New label';
$entity->save();
$this->assertHookMessageOrder(array('entity_crud_hook_test_block_presave called', 'entity_crud_hook_test_entity_presave called for type block', 'entity_crud_hook_test_block_update called', 'entity_crud_hook_test_entity_update called for type block'));
$GLOBALS['entity_crud_hook_test'] = array();
$entity->delete();
$this->assertHookMessageOrder(array('entity_crud_hook_test_block_predelete called', 'entity_crud_hook_test_entity_predelete called for type block', 'entity_crud_hook_test_block_delete called', 'entity_crud_hook_test_entity_delete called for type block'));
}
示例8: process
/**
* {@inheritdoc}
*/
public function process($text, $langcode)
{
$new_text = $text;
$filter_result = new FilterProcessResult($text);
if (preg_match('/\\[survey\\:.+\\]/', $text, $result)) {
$token = $result[0];
$start = strpos($token, ':') + 1;
$length = strpos($token, ']') - $start;
$id = substr($token, $start, $length);
$block = Block::load($id);
if ($block) {
$replace = $block->get('settings')['html'];
$new_text = str_replace($token, $replace, $text);
$filter_result->setProcessedText($new_text);
$filter_result->setCacheTags($block->getCacheTags());
}
}
return $filter_result;
}
示例9: testLanguageBlockVisibilityLanguageDelete
/**
* Tests if the visibility settings are removed if the language is deleted.
*/
public function testLanguageBlockVisibilityLanguageDelete()
{
// Enable a standard block and set the visibility setting for one language.
$edit = array('visibility' => array('language' => array('langcodes' => array('fr' => 'fr'))));
$block = $this->drupalPlaceBlock('system_powered_by_block', $edit);
// Check that we have the language in config after saving the setting.
$visibility = $block->getVisibility();
$this->assertEqual('fr', $visibility['language']['langcodes']['fr'], 'Language is set in the block configuration.');
// Delete the language.
$this->drupalPostForm('admin/config/regional/language/delete/fr', array(), t('Delete'));
// Check that the language is no longer stored in the configuration after
// it is deleted.
$block = Block::load($block->id());
$visibility = $block->getVisibility();
$this->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.');
// Ensure that the block visibility for language is gone from the UI.
$this->drupalGet('admin/structure/block');
$this->clickLink('Configure');
$elements = $this->xpath('//details[@id="edit-visibility-language"]');
$this->assertTrue(empty($elements));
}
示例10: testBlockInInvalidRegion
/**
* Tests that blocks assigned to invalid regions work correctly.
*/
function testBlockInInvalidRegion()
{
// Enable a test block and place it in an invalid region.
$block = $this->drupalPlaceBlock('test_html');
$block->setRegion('invalid_region');
$block->save();
$warning_message = t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $block->id(), '%region' => 'invalid_region'));
// Clearing the cache should disable the test block placed in the invalid region.
$this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches');
$this->assertRaw($warning_message, 'Enabled block was in the invalid region and has been disabled.');
// Clear the cache to check if the warning message is not triggered.
$this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches');
$this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.');
// Place disabled test block in the invalid region of the default theme.
$block = Block::load($block->id());
$block->setRegion('invalid_region');
$block->save();
// Clear the cache to check if the warning message is not triggered.
$this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches');
$this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.');
}
示例11: assertEntity
/**
* Asserts various aspects of a block.
*
* @param string $id
* The block ID.
* @param string $plugin_id
* The block's plugin ID.
* @param array $roles
* Role IDs the block is expected to have.
* @param string $pages
* The list of pages on which the block should appear.
* @param string $region
* The display region.
* @param string $theme
* The theme.
* @param string $weight
* The block weight.
* @param string $label
* The block label.
* @param string $label_display
* The block label display setting.
* @param bool $status
* (optional) Whether the block is expected to be enabled.
*/
public function assertEntity($id, $plugin_id, array $roles, $pages, $region, $theme, $weight, $label, $label_display, $status = TRUE)
{
$block = Block::load($id);
$this->assertTrue($block instanceof Block);
/** @var \Drupal\block\BlockInterface $block */
$this->assertSame($plugin_id, $block->getPluginId());
$visibility = $block->getVisibility();
if ($roles) {
$this->assertSame($roles, array_values($visibility['user_role']['roles']));
$this->assertSame('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
}
if ($pages) {
$this->assertSame($pages, $visibility['request_path']['pages']);
}
$this->assertSame($region, $block->getRegion());
$this->assertSame($theme, $block->getTheme());
$this->assertSame($weight, $block->getWeight());
$this->assertSame($status, $block->status());
$config = $this->config('block.block.' . $id);
$this->assertSame($label, $config->get('settings.label'));
$this->assertSame($label_display, $config->get('settings.label_display'));
}
示例12: doTestMenuBlock
/**
* Tests menu block settings.
*/
protected function doTestMenuBlock()
{
$menu_id = $this->menu->id();
$block_id = $this->blockPlacements[$menu_id];
$this->drupalGet('admin/structure/block/manage/' . $block_id);
$this->drupalPostForm(NULL, ['settings[depth]' => 3, 'settings[level]' => 2], t('Save block'));
$block = Block::load($block_id);
$settings = $block->getPlugin()->getConfiguration();
$this->assertEqual($settings['depth'], 3);
$this->assertEqual($settings['level'], 2);
// Reset settings.
$block->getPlugin()->setConfigurationValue('depth', 0);
$block->getPlugin()->setConfigurationValue('level', 1);
$block->save();
}
示例13: testRecentNodeBlock
/**
* Tests the recent comments block.
*/
public function testRecentNodeBlock()
{
$this->drupalLogin($this->adminUser);
// Disallow anonymous users to view content.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access content' => FALSE));
// Enable the recent content block with two items.
$block = $this->drupalPlaceBlock('views_block:content_recent-block_1', array('id' => 'test_block', 'items_per_page' => 2));
// Test that block is not visible without nodes.
$this->drupalGet('');
$this->assertText(t('No content available.'), 'Block with "No content available." found.');
// Add some test nodes.
$default_settings = array('uid' => $this->webUser->id(), 'type' => 'article');
$node1 = $this->drupalCreateNode($default_settings);
$node2 = $this->drupalCreateNode($default_settings);
$node3 = $this->drupalCreateNode($default_settings);
// Change the changed time for node so that we can test ordering.
db_update('node_field_data')->fields(array('changed' => $node1->getChangedTime() + 100))->condition('nid', $node2->id())->execute();
db_update('node_field_data')->fields(array('changed' => $node1->getChangedTime() + 200))->condition('nid', $node3->id())->execute();
// Test that a user without the 'access content' permission cannot
// see the block.
$this->drupalLogout();
$this->drupalGet('');
$this->assertNoText($block->label(), 'Block was not found.');
// Test that only the 2 latest nodes are shown.
$this->drupalLogin($this->webUser);
$this->assertNoText($node1->label(), 'Node not found in block.');
$this->assertText($node2->label(), 'Node found in block.');
$this->assertText($node3->label(), 'Node found in block.');
// Check to make sure nodes are in the right order.
$this->assertTrue($this->xpath('//div[@id="block-test-block"]//table/tbody/tr[position() = 1]/td/a[text() = "' . $node3->label() . '"]'), 'Nodes were ordered correctly in block.');
$this->drupalLogout();
$this->drupalLogin($this->adminUser);
// Verify that the More link is shown and leads to the admin content page.
$this->drupalGet('');
$this->clickLink('More');
$this->assertResponse('200');
$this->assertUrl('admin/content');
// Set the number of recent nodes to show to 10.
$block->getPlugin()->setConfigurationValue('items_per_page', 10);
$block->save();
// Post an additional node.
$node4 = $this->drupalCreateNode($default_settings);
// Test that all four nodes are shown.
$this->drupalGet('');
$this->assertText($node1->label(), 'Node found in block.');
$this->assertText($node2->label(), 'Node found in block.');
$this->assertText($node3->label(), 'Node found in block.');
$this->assertText($node4->label(), 'Node found in block.');
// Enable the "Powered by Drupal" block only on article nodes.
$edit = ['id' => strtolower($this->randomMachineName()), 'region' => 'sidebar_first', 'visibility[node_type][bundles][article]' => 'article'];
$theme = \Drupal::service('theme_handler')->getDefault();
$this->drupalPostForm("admin/structure/block/add/system_powered_by_block/{$theme}", $edit, t('Save block'));
$block = Block::load($edit['id']);
$visibility = $block->getVisibility();
$this->assertTrue(isset($visibility['node_type']['bundles']['article']), 'Visibility settings were saved to configuration');
// Create a page node.
$node5 = $this->drupalCreateNode(array('uid' => $this->adminUser->id(), 'type' => 'page'));
$this->drupalLogout();
$this->drupalLogin($this->webUser);
// Verify visibility rules.
$this->drupalGet('');
$label = $block->label();
$this->assertNoText($label, 'Block was not displayed on the front page.');
$this->drupalGet('node/add/article');
$this->assertText($label, 'Block was displayed on the node/add/article page.');
$this->drupalGet('node/' . $node1->id());
$this->assertText($label, 'Block was displayed on the node/N when node is of type article.');
$this->drupalGet('node/' . $node5->id());
$this->assertNoText($label, 'Block was not displayed on nodes of type page.');
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/structure/block');
$this->assertText($label, 'Block was displayed on the admin/structure/block page.');
$this->assertLinkByHref($block->url());
}
示例14: findAndReplaceEntityReference
/**
* Replacing value for field entity reference
*
* @param $crawler, $field_name, $field, $entity
* crawler object
* field name
* field array
* entity
*
* @return
* render crawler object/render markup after replacing value
*/
public function findAndReplaceEntityReference($crawler, $field_name, $field, $entity)
{
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$field_instance = FieldConfig::loadByName($entity_type, $bundle, $bundle . '_' . $field_name);
$settings = $field_instance->getSettings();
switch ($settings['handler']) {
case 'default:view':
$view_render_array = views_embed_view($field['target_id']);
$crawler->filter('[data-field="' . $field_name . '"]')->append(drupal_render($view_render_array)->__toString());
break;
case 'default:block':
$block = \Drupal\block\Entity\Block::load($field['target_id']);
$block_content = \Drupal::entityManager()->getViewBuilder('block')->view($block);
$crawler->filter('[data-field="' . $field_name . '"]')->append(drupal_render($block_content)->__toString());
break;
case 'default:contact_form':
$form_id = $field['content']['target_id'];
$message = \Drupal::entityManager()->getStorage('contact_message')->create(array('contact_form' => $form_id));
$render_markup = \Drupal::service('entity.form_builder')->getForm($message);
$markup = drupal_render($render_markup);
$crawler->filter('[data-field="' . $field_name . '"]')->replaceWith($markup->__toString());
break;
default:
break;
}
}
示例15: testDisableLanguageSwitcher
/**
* Tests if the language switcher block gets deleted when a language type has been made not configurable.
*/
public function testDisableLanguageSwitcher()
{
$block_id = 'test_language_block';
// Enable the language switcher block.
$this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_CONTENT, array('id' => $block_id));
// Check if the language switcher block has been created.
$block = Block::load($block_id);
$this->assertTrue($block, 'Language switcher block was created.');
// Make sure language_content is not configurable.
$edit = array('language_content[configurable]' => FALSE);
$this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$this->assertResponse(200);
// Check if the language switcher block has been removed.
$block = Block::load($block_id);
$this->assertFalse($block, 'Language switcher block was removed.');
}