/** * Change the view mode of an entity that is being displayed. * * @param string $view_mode * The view_mode that is to be used to display the entity. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity that is being viewed. * @param array $context * Array with additional context information, currently only contains the * langcode the entity is viewed in. * * @ingroup entity_crud */ function hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) { // For nodes, change the view mode when it is teaser. if ($entity->getEntityTypeId() == 'node' && $view_mode == 'teaser') { $view_mode = 'my_custom_view_mode'; } }
/** * Control download access to files. * * The hook is typically implemented to limit access based on the entity that * references the file; for example, only users with access to a node should be * allowed to download files attached to that node. * * @param $field * The field to which the file belongs. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity which references the file. * @param \Drupal\file\FileInterface $file * The file entity that is being requested. * * @return * TRUE is access should be allowed by this entity or FALSE if denied. Note * that denial may be overridden by another entity controller, making this * grant permissive rather than restrictive. * * @see hook_entity_field_access(). */ function hook_file_download_access($field, Drupal\Core\Entity\EntityInterface $entity, Drupal\file\FileInterface $file) { if ($entity->getEntityTypeId() == 'node') { return $entity->access('view'); } }