/** * Say hello to the world. * * @return string * Return "Hello world!" string. */ public function helloWorld(EntityViewModeInterface $viewmode) { $content = ['#type' => 'markup', '#markup' => $this->t('Hello world!')]; // Second version displaying the opening hours of the library. $opening_hours = $this->config('happy_alexandrie.library_config')->get('opening_hours'); if (!empty($opening_hours)) { $content = ['#markup' => $this->t('<p>Greetings dear adventurer!</p><p>Opening hours:<br />@opening_hours</p>', array('@opening_hours' => $opening_hours))]; } // Third version with the query // Query against our entities. $query = $this->query_factory->get('node')->condition('status', 1)->condition('type', 'alexandrie_book')->condition('changed', REQUEST_TIME, '<')->range(0, 5); $nids = $query->execute(); if ($nids) { // Load the storage manager of our entity. $storage = $this->entity_manager->getStorage('node'); // Now we can load the entities. $nodes = $storage->loadMultiple($nids); list($entity_type, $viewmode_name) = explode('.', $viewmode->getOriginalId()); // Get the EntityViewBuilder instance. $render_controller = $this->entity_manager->getViewBuilder('node'); $build = $render_controller->viewMultiple($nodes, $viewmode_name); $build['#markup'] = $this->t('Happy Query by view mode: @label', array('@label' => $viewmode->label())); $content[] = $build; } else { $content[] = array('#markup' => $this->t('No result')); } return $content; }
/** * {@inheritdoc} */ public function build() { $node = $this->entity_manager->getStorage('node')->load($this->configuration['block_node_id']); if (!$node) { return ''; } $this->setViewMode($this->configuration['block_node_view_mode']); $view_builder = $this->entity_manager->getViewBuilder('node'); return $view_builder->view($node, $this->configuration['block_node_view_mode']); }