Exemplo n.º 1
0
/**
 * 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');
    }
}
Exemplo n.º 2
0
/**
 * 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';
    }
}
Exemplo n.º 3
0
/**
 * Returns a renderable array for the value of a single field in an entity.
 *
 * To integrate with in-place field editing when a non-standard render pipeline
 * is used (FieldItemListInterface::view() is not sufficient to render back the
 * field following in-place editing in the exact way it was displayed
 * originally), implement this hook.
 *
 * Edit module integrates with HTML elements with data-edit-field-id attributes.
 * For example:
 *   data-edit-field-id="node/1/<field-name>/und/<module-name>-<custom-id>"
 * After the editing is complete, this hook is invoked on the module with
 * the custom render pipeline identifier (last part of data-edit-field-id) to
 * re-render the field. Use the same logic used when rendering the field for
 * the original display.
 *
 * The implementation should take care of invoking the prepare_view steps. It
 * should also respect field access permissions.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity containing the field to display.
 * @param string $field_name
 *   The name of the field to display.
 * @param string $view_mode_id
 *   View mode ID for the custom render pipeline this field view was destined
 *   for. This is not a regular view mode ID for the Entity/Field API render
 *   pipeline and is provided by the renderer module instead. An example could
 *   be Views' render pipeline. In the example of Views, the view mode ID would
 *   probably contain the View's ID, display and the row index. Views would
 *   know the internal structure of this ID. The only structure imposed on this
 *   ID is that it contains dash separated values and the first value is the
 *   module name. Only that module's hook implementation will be invoked. Eg.
 *   'views-...-...'.
 * @param string $langcode
 *   (Optional) The language code the field values are to be shown in.
 *
 * @return
 *   A renderable array for the field value.
 *
 * @see \Drupal\Core\Field\FieldItemListInterface::view()
 */
function hook_quickedit_render_field(Drupal\Core\Entity\EntityInterface $entity, $field_name, $view_mode_id, $langcode)
{
    return array('#prefix' => '<div class="example-markup">', 'field' => $entity->getTranslation($langcode)->get($field_name)->view($view_mode_id), '#suffix' => '</div>');
}