X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fctools%2Fsrc%2FPlugin%2FBlock%2FEntityView.php;fp=web%2Fmodules%2Fcontrib%2Fctools%2Fsrc%2FPlugin%2FBlock%2FEntityView.php;h=51342d010c02271d8fa8b3221879c8774190e6c8;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/ctools/src/Plugin/Block/EntityView.php b/web/modules/contrib/ctools/src/Plugin/Block/EntityView.php new file mode 100644 index 000000000..51342d010 --- /dev/null +++ b/web/modules/contrib/ctools/src/Plugin/Block/EntityView.php @@ -0,0 +1,105 @@ +entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'view_mode' => 'default', + ]; + } + + /** + * {@inheritdoc} + */ + public function blockForm($form, FormStateInterface $form_state) { + $form['view_mode'] = [ + '#type' => 'select', + '#options' => $this->entityManager->getViewModeOptions($this->getDerivativeId()), + '#title' => $this->t('View mode'), + '#default_value' => $this->configuration['view_mode'], + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function blockSubmit($form, FormStateInterface $form_state) { + $this->configuration['view_mode'] = $form_state->getValue('view_mode'); + } + + /** + * {@inheritdoc} + */ + public function build() { + /** @var $entity \Drupal\Core\Entity\EntityInterface */ + $entity = $this->getContextValue('entity'); + + $view_builder = $this->entityManager->getViewBuilder($entity->getEntityTypeId()); + $build = $view_builder->view($entity, $this->configuration['view_mode']); + + CacheableMetadata::createFromObject($this->getContext('entity')) + ->applyTo($build); + + return $build; + } + +}