/** * {@inheritdoc} */ public function execute($comment = NULL) { $build = $this->viewBuilder->view($comment); $text = $this->renderer->renderPlain($build); foreach ($this->configuration['keywords'] as $keyword) { if (strpos($text, $keyword) !== FALSE) { $comment->setPublished(FALSE); $comment->save(); break; } } }
/** * {@inheritdoc} */ public function build() { // Track whether a block that shows the main content is displayed or not. $main_content_block_displayed = FALSE; $build = array(); // Load all region content assigned via blocks. foreach ($this->getRegionAssignments() as $region => $blocks) { /** @var $blocks \Drupal\block\BlockInterface[] */ foreach ($blocks as $key => $block) { if ($block->access('view')) { $block_plugin = $block->getPlugin(); if ($block_plugin instanceof MainContentBlockPluginInterface) { $block_plugin->setMainContent($this->mainContent); $main_content_block_displayed = TRUE; } $build[$region][$key] = $this->blockViewBuilder->view($block); } } if (!empty($build[$region])) { // self::getRegionAssignments() returns the blocks in sorted order. $build[$region]['#sorted'] = TRUE; } } // If no block that shows the main content is displayed, still show the main // content. Otherwise the end user will see all displayed blocks, but not // the main content they came for. if (!$main_content_block_displayed) { $build['content']['system_main'] = $this->mainContent; } return $build; }
/** * Generates printer-friendly HTML for a node. * * @param \Drupal\node\NodeInterface $node * The node that will be output. * @param string $children * (optional) All the rendered child nodes within the current node. Defaults * to an empty string. * * @return array * A render array for the exported HTML of a given node. * * @see \Drupal\book\BookExport::exportTraverse() */ protected function bookNodeExport(NodeInterface $node, $children = '') { $build = $this->viewBuilder->view($node, 'print', NULL); unset($build['#theme']); // @todo Rendering should happen in the template using render(). $node->rendered = drupal_render($build); return array('#theme' => 'book_node_export_html', '#node' => $node, '#children' => $children); }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { $elements = array(); $output = array(); $field_name = $this->fieldDefinition->getName(); $entity = $items->getEntity(); $status = $items->status; if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) { $comment_settings = $this->getFieldSettings(); // Only attempt to render comments if the entity has visible comments. // Unpublished comments are not included in // $entity->get($field_name)->comment_count, but unpublished comments // should display if the user is an administrator. $elements['#cache']['contexts'][] = 'user.roles'; if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) { // This is a listing of Comment entities, so associate its list cache // tag for correct invalidation. $output['comments']['#cache']['tags'] = $this->entityManager->getDefinition('comment')->getListCacheTags(); if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) { $mode = $comment_settings['default_mode']; $comments_per_page = $comment_settings['per_page']; $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); if ($comments) { comment_prepare_thread($comments); $build = $this->viewBuilder->viewMultiple($comments); $build['pager']['#type'] = 'pager'; if ($this->getSetting('pager_id')) { $build['pager']['#element'] = $this->getSetting('pager_id'); } $output['comments'] += $build; } } } // Append comment form if the comments are open and the form is set to // display below the entity. Do not show the form for the print view mode. if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') { // Only show the add comment form if the user has permission. $elements['#cache']['contexts'][] = 'user.roles'; if ($this->currentUser->hasPermission('post comments')) { // All users in the "anonymous" role can use the same form: it is fine // for this form to be stored in the render cache. if ($this->currentUser->isAnonymous()) { $comment = $this->storage->create(array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $this->getFieldSetting('comment_type'), 'pid' => NULL)); $output['comment_form'] = $this->entityFormBuilder->getForm($comment); } else { $callback = 'comment.post_render_cache:renderForm'; $context = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name); $placeholder = drupal_render_cache_generate_placeholder($callback, $context); $output['comment_form'] = array('#post_render_cache' => array($callback => array($context)), '#markup' => $placeholder); } } } $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array()); } return $elements; }
/** * Tests the building of a full page variant. * * @covers ::build * @covers ::getRegionAssignments */ public function testBuild() { $theme = $this->randomMachineName(); $display_variant = $this->setUpDisplayVariant(); $this->themeNegotiator->expects($this->any())->method('determineActiveTheme')->with($this->routeMatch)->will($this->returnValue($theme)); $display_variant->expects($this->once())->method('getRegionNames')->will($this->returnValue(array('top' => 'Top', 'bottom' => 'Bottom'))); $blocks_config = array('block1' => array(TRUE, 'top', 0), 'block2' => array(FALSE, 'bottom', 0), 'block3' => array(TRUE, 'bottom', 5), 'block4' => array(TRUE, 'bottom', -5)); $blocks = array(); foreach ($blocks_config as $block_id => $block_config) { $block = $this->getMock('Drupal\\block\\BlockInterface'); $block->expects($this->once())->method('access')->will($this->returnValue($block_config[0])); $block->expects($this->any())->method('get')->will($this->returnValueMap(array(array('region', $block_config[1]), array('weight', $block_config[2]), array('status', TRUE)))); $blocks[$block_id] = $block; } $this->blockViewBuilder->expects($this->exactly(3))->method('view')->will($this->returnValue(array())); $this->blockStorage->expects($this->once())->method('loadByProperties')->with(array('theme' => $theme))->will($this->returnValue($blocks)); $expected = array('top' => array('block1' => array(), '#sorted' => TRUE), 'bottom' => array('block4' => array(), 'block3' => array(), '#sorted' => TRUE)); $this->assertSame($expected, $display_variant->build()); }
/** * {@inheritdoc} */ public function build() { // Track whether blocks showing the main content and messages are displayed. $main_content_block_displayed = FALSE; $messages_block_displayed = FALSE; $build = ['#cache' => ['tags' => $this->blockListCacheTags]]; // Load all region content assigned via blocks. $cacheable_metadata_list = []; foreach ($this->blockRepository->getVisibleBlocksPerRegion($cacheable_metadata_list) as $region => $blocks) { /** @var $blocks \Drupal\block\BlockInterface[] */ foreach ($blocks as $key => $block) { $block_plugin = $block->getPlugin(); if ($block_plugin instanceof MainContentBlockPluginInterface) { $block_plugin->setMainContent($this->mainContent); $main_content_block_displayed = TRUE; } elseif ($block_plugin instanceof MessagesBlockPluginInterface) { $messages_block_displayed = TRUE; } $build[$region][$key] = $this->blockViewBuilder->view($block); // The main content block cannot be cached: it is a placeholder for the // render array returned by the controller. It should be rendered as-is, // with other placed blocks "decorating" it. if ($block_plugin instanceof MainContentBlockPluginInterface) { unset($build[$region][$key]['#cache']['keys']); } } if (!empty($build[$region])) { // \Drupal\block\BlockRepositoryInterface::getVisibleBlocksPerRegion() // returns the blocks in sorted order. $build[$region]['#sorted'] = TRUE; } } // If no block that shows the main content is displayed, still show the main // content. Otherwise the end user will see all displayed blocks, but not // the main content they came for. if (!$main_content_block_displayed) { $build['content']['system_main'] = $this->mainContent; } // If no block displays status messages, still render them. if (!$messages_block_displayed) { $build['content']['messages'] = ['#weight' => -1000, '#type' => 'status_messages']; } // The access results' cacheability is currently added to the top level of the // render array. This is done to prevent issues with empty regions being // displayed. // This would need to be changed to allow caching of block regions, as each // region must then have the relevant cacheable metadata. $merged_cacheable_metadata = CacheableMetadata::createFromRenderArray($build); foreach ($cacheable_metadata_list as $cacheable_metadata) { $merged_cacheable_metadata = $merged_cacheable_metadata->merge($cacheable_metadata); } $merged_cacheable_metadata->applyTo($build); return $build; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { $elements = array(); $output = array(); $field_name = $this->fieldDefinition->getName(); $entity = $items->getEntity(); $status = $items->status; if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) { $comment_settings = $this->getFieldSettings(); // Only attempt to render comments if the entity has visible comments. // Unpublished comments are not included in // $entity->get($field_name)->comment_count, but unpublished comments // should display if the user is an administrator. $elements['#cache']['contexts'][] = 'user.permissions'; if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) { $output['comments'] = []; if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) { $mode = $comment_settings['default_mode']; $comments_per_page = $comment_settings['per_page']; $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); if ($comments) { $build = $this->viewBuilder->viewMultiple($comments); $build['pager']['#type'] = 'pager'; if ($this->getSetting('pager_id')) { $build['pager']['#element'] = $this->getSetting('pager_id'); } $output['comments'] += $build; } } } // Append comment form if the comments are open and the form is set to // display below the entity. Do not show the form for the print view mode. if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') { // Only show the add comment form if the user has permission. $elements['#cache']['contexts'][] = 'user.roles'; if ($this->currentUser->hasPermission('post comments')) { $output['comment_form'] = ['#lazy_builder' => ['comment.lazy_builders:renderForm', [$entity->getEntityTypeId(), $entity->id(), $field_name, $this->getFieldSetting('comment_type')]]]; // @todo Remove this in https://www.drupal.org/node/2543334. Until // then, \Drupal\Core\Render\Renderer::hasPoorCacheability() isn't // integrated with cache context bubbling, so this duplicates the // contexts added by \Drupal\comment\CommentForm::form(). $output['comment_form']['#cache']['contexts'][] = 'user.permissions'; $output['comment_form']['#cache']['contexts'][] = 'user.roles:authenticated'; if ($this->currentUser->isAuthenticated()) { $output['comment_form']['#cache']['contexts'][] = 'user'; } } } $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array()); } return $elements; }
/** * @covers ::render * @covers ::defineOptions * @covers ::init */ public function testRenderWithUuid() { $this->setupEntityManager(); $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4'; $options = ['target' => $uuid, 'tokenize' => FALSE]; $entity = $this->getMock('Drupal\\Core\\Entity\\EntityInterface'); $entity->expects($this->once())->method('access')->willReturn(TRUE); $this->entityStorage->expects($this->never())->method('load'); $this->entityManager->expects($this->once())->method('loadEntityByConfigTarget')->willReturn($entity); $this->entityViewBuilder->expects($this->once())->method('view')->with($entity, 'default')->willReturn(['#markup' => 'hallo']); $this->entityHandler->init($this->executable, $this->display, $options); $result = $this->entityHandler->render(); $this->assertEquals(['#markup' => 'hallo'], $result); }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = array(); $output = array(); $field_name = $this->fieldDefinition->getName(); $entity = $items->getEntity(); $status = $items->status; if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) { $comment_settings = $this->getFieldSettings(); // Only attempt to render comments if the entity has visible comments. // Unpublished comments are not included in // $entity->get($field_name)->comment_count, but unpublished comments // should display if the user is an administrator. $elements['#cache']['contexts'][] = 'user.permissions'; if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) { $output['comments'] = []; if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) { $mode = $comment_settings['default_mode']; $comments_per_page = $comment_settings['per_page']; $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); if ($comments) { $build = $this->viewBuilder->viewMultiple($comments); $build['pager']['#type'] = 'pager'; // CommentController::commentPermalink() calculates the page number // where a specific comment appears and does a subrequest pointing to // that page, we need to pass that subrequest route to our pager to // keep the pager working. $build['pager']['#route_name'] = $this->routeMatch->getRouteObject(); $build['pager']['#route_parameters'] = $this->routeMatch->getRawParameters()->all(); if ($this->getSetting('pager_id')) { $build['pager']['#element'] = $this->getSetting('pager_id'); } $output['comments'] += $build; } } } // Append comment form if the comments are open and the form is set to // display below the entity. Do not show the form for the print view mode. if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') { // Only show the add comment form if the user has permission. $elements['#cache']['contexts'][] = 'user.roles'; if ($this->currentUser->hasPermission('post comments')) { $output['comment_form'] = ['#lazy_builder' => ['comment.lazy_builders:renderForm', [$entity->getEntityTypeId(), $entity->id(), $field_name, $this->getFieldSetting('comment_type')]], '#create_placeholder' => TRUE]; } } $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array()); } return $elements; }
/** * Tests the building of a full page variant. * * @covers ::build * * @dataProvider providerBuild */ public function testBuild(array $blocks_config, $visible_block_count, array $expected_render_array) { $display_variant = $this->setUpDisplayVariant(); $display_variant->setMainContent(['#markup' => 'Hello kittens!']); $blocks = ['top' => [], 'center' => [], 'bottom' => []]; $block_plugin = $this->getMock('Drupal\\Core\\Block\\BlockPluginInterface'); $main_content_block_plugin = $this->getMock('Drupal\\Core\\Block\\MainContentBlockPluginInterface'); $messages_block_plugin = $this->getMock('Drupal\\Core\\Block\\MessagesBlockPluginInterface'); foreach ($blocks_config as $block_id => $block_config) { $block = $this->getMock('Drupal\\block\\BlockInterface'); $block->expects($this->atLeastOnce())->method('getPlugin')->willReturn($block_config[1] ? $main_content_block_plugin : ($block_config[2] ? $messages_block_plugin : $block_plugin)); $blocks[$block_config[0]][$block_id] = $block; } $this->blockViewBuilder->expects($this->exactly($visible_block_count))->method('view')->will($this->returnValue(array())); $this->blockRepository->expects($this->once())->method('getVisibleBlocksPerRegion')->will($this->returnValue($blocks)); $this->assertSame($expected_render_array, $display_variant->build()); }
/** * {@inheritdoc} */ public function build() { $build = array(); // Load all region content assigned via blocks. foreach ($this->getRegionAssignments() as $region => $blocks) { /** @var $blocks \Drupal\block\BlockInterface[] */ foreach ($blocks as $key => $block) { if ($block->access('view')) { $build[$region][$key] = $this->blockViewBuilder->view($block); } } if (!empty($build[$region])) { // self::getRegionAssignments() returns the blocks in sorted order. $build[$region]['#sorted'] = TRUE; } } return $build; }
/** * {@inheritdoc} */ public function build() { // Track whether blocks showing the main content and messages are displayed. $main_content_block_displayed = FALSE; $messages_block_displayed = FALSE; $build = ['#cache' => ['tags' => $this->blockListCacheTags]]; $contexts = $this->getActiveBlockContexts(); // Load all region content assigned via blocks. foreach ($this->blockRepository->getVisibleBlocksPerRegion($contexts) as $region => $blocks) { /** @var $blocks \Drupal\block\BlockInterface[] */ foreach ($blocks as $key => $block) { $block_plugin = $block->getPlugin(); if ($block_plugin instanceof MainContentBlockPluginInterface) { $block_plugin->setMainContent($this->mainContent); $main_content_block_displayed = TRUE; } elseif ($block_plugin instanceof MessagesBlockPluginInterface) { $messages_block_displayed = TRUE; } $build[$region][$key] = $this->blockViewBuilder->view($block); // The main content block cannot be cached: it is a placeholder for the // render array returned by the controller. It should be rendered as-is, // with other placed blocks "decorating" it. if ($block_plugin instanceof MainContentBlockPluginInterface) { unset($build[$region][$key]['#cache']['keys']); } } if (!empty($build[$region])) { // \Drupal\block\BlockRepositoryInterface::getVisibleBlocksPerRegion() // returns the blocks in sorted order. $build[$region]['#sorted'] = TRUE; } } // If no block that shows the main content is displayed, still show the main // content. Otherwise the end user will see all displayed blocks, but not // the main content they came for. if (!$main_content_block_displayed) { $build['content']['system_main'] = $this->mainContent; } // If no block displays status messages, still render them. if (!$messages_block_displayed) { $build['content']['messages'] = ['#weight' => -1000, '#type' => 'status_messages']; } return $build; }
/** * Tests the building of a full page variant. * * @covers ::build * @covers ::getRegionAssignments * * @dataProvider providerBuild */ public function testBuild(array $blocks_config, $visible_block_count, array $expected_render_array) { $theme = $this->randomMachineName(); $display_variant = $this->setUpDisplayVariant(); $this->themeNegotiator->expects($this->any())->method('determineActiveTheme')->with($this->routeMatch)->will($this->returnValue($theme)); $display_variant->expects($this->once())->method('getRegionNames')->will($this->returnValue(array('top' => 'Top', 'center' => 'Center', 'bottom' => 'Bottom'))); $display_variant->setMainContent(['#markup' => 'Hello kittens!']); $blocks = array(); $block_plugin = $this->getMock('Drupal\\Core\\Block\\BlockPluginInterface'); $main_content_block_plugin = $this->getMock('Drupal\\Core\\Block\\MainContentBlockPluginInterface'); foreach ($blocks_config as $block_id => $block_config) { $block = $this->getMock('Drupal\\block\\BlockInterface'); $block->expects($this->once())->method('access')->will($this->returnValue($block_config[0])); $block->expects($this->any())->method('get')->will($this->returnValueMap(array(array('region', $block_config[1]), array('weight', $block_config[2]), array('status', TRUE)))); $block->expects($this->any())->method('getPlugin')->willReturn($block_config[3] ? $main_content_block_plugin : $block_plugin); $blocks[$block_id] = $block; } $this->blockViewBuilder->expects($this->exactly($visible_block_count))->method('view')->will($this->returnValue(array())); $this->blockStorage->expects($this->once())->method('loadByProperties')->with(array('theme' => $theme))->will($this->returnValue($blocks)); $this->assertSame($expected_render_array, $display_variant->build()); }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { $elements = array(); $output = array(); $field_name = $this->fieldDefinition->getName(); $entity = $items->getEntity(); $status = $items->status; if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) { $comment_settings = $this->getFieldSettings(); // Only attempt to render comments if the entity has visible comments. // Unpublished comments are not included in // $entity->get($field_name)->comment_count, but unpublished comments // should display if the user is an administrator. if ($entity->get($field_name)->comment_count && $this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) { $mode = $comment_settings['default_mode']; $comments_per_page = $comment_settings['per_page']; $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id')); if ($comments) { comment_prepare_thread($comments); $build = $this->viewBuilder->viewMultiple($comments); $build['pager']['#theme'] = 'pager'; if ($this->getSetting('pager_id')) { $build['pager']['#element'] = $this->getSetting('pager_id'); } // The viewElements() method of entity field formatters is run // during the #pre_render phase of rendering an entity. A formatter // builds the content of the field in preparation for theming. // All entity cache tags must be available after the #pre_render phase. // This field formatter is highly exceptional: it renders *another* // entity and this referenced entity has its own #pre_render // callbacks. In order collect the cache tags associated with the // referenced entity it must be passed to drupal_render() so that its // #pre_render callbacks are invoked and its full build array is // assembled. Rendering the referenced entity in place here will allow // its cache tags to be bubbled up and included with those of the // main entity when cache tags are collected for a renderable array // in drupal_render(). drupal_render($build, TRUE); $output['comments'] = $build; } } // Append comment form if the comments are open and the form is set to // display below the entity. Do not show the form for the print view mode. if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW && $this->viewMode != 'print') { // Only show the add comment form if the user has permission. if ($this->currentUser->hasPermission('post comments')) { // All users in the "anonymous" role can use the same form: it is fine // for this form to be stored in the render cache. if ($this->currentUser->isAnonymous()) { $comment = $this->storage->create(array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $this->getFieldSetting('comment_type'), 'pid' => NULL)); $output['comment_form'] = $this->entityFormBuilder->getForm($comment); } else { $callback = 'comment.post_render_cache:renderForm'; $context = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name); $placeholder = drupal_render_cache_generate_placeholder($callback, $context); $output['comment_form'] = array('#post_render_cache' => array($callback => array($context)), '#markup' => $placeholder); } } } $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array()); } return $elements; }
/** * Generates printer-friendly HTML for a node. * * @param \Drupal\node\NodeInterface $node * The node that will be output. * @param string $children * (optional) All the rendered child nodes within the current node. Defaults * to an empty string. * * @return array * A render array for the exported HTML of a given node. * * @see \Drupal\book\BookExport::exportTraverse() */ protected function bookNodeExport(NodeInterface $node, $children = '') { $build = $this->viewBuilder->view($node, 'print', NULL); unset($build['#theme']); return array('#theme' => 'book_node_export_html', '#content' => $build, '#node' => $node, '#children' => $children); }