entity_print.module in Entity Print 8
Same filename and directory in other branches
Print any entity.
File
entity_print.moduleView source
<?php
/**
* @file
* Print any entity.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Masterminds\HTML5;
/**
* Implements hook_theme().
*/
function entity_print_theme($existing, $type, $theme, $path) {
return array(
'entity_print' => array(
'path' => $path . '/templates',
'template' => 'entity-print',
'variables' => array(
'entity_array' => NULL,
'entity' => NULL,
'entity_print_css' => NULL,
),
),
);
}
/**
* Implements hook_entity_extra_field_info_alter().
*/
function entity_print_entity_extra_field_info_alter(&$info) {
foreach ($info as &$entity_info) {
foreach ($entity_info as &$bundle_info) {
$bundle_info['display']['entity_print_view'] = [
'label' => t('Entity Print'),
'description' => t('Provides a link to view the PDF version of the entity'),
'weight' => 0,
'visible' => FALSE,
];
}
}
}
/**
* Implements hook_entity_view_alter().
*/
function entity_print_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($display
->getComponent('entity_print_view')) {
$route_params = [
'entity_type' => $entity
->getEntityTypeId(),
'entity_id' => $entity
->id(),
];
$build['entity_print_view'] = Link::createFromRoute($display
->getThirdPartySetting('entity_print', 'label', t('View PDF')), 'entity_print.view', $route_params)
->toRenderable();
// Add the access control.
$build['entity_print_view']['#access'] = \Drupal::accessManager()
->checkNamedRoute('entity_print.view', $route_params);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function entity_print_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
$display = $form_state
->getFormObject()
->getEntity();
$form['fields']['entity_print_view']['empty_cell'] = [
'#type' => 'textfield',
'#title' => '',
'#default_value' => $display
->getThirdPartySetting('entity_print', 'label', t('View PDF')),
];
$form['#validate'][] = 'entity_print_form_entity_view_display_edit_form_validate';
$form['actions']['submit']['#submit'][] = 'entity_print_form_entity_view_display_edit_form_submit';
}
/**
* Entity display form validation handler.
*/
function entity_print_form_entity_view_display_edit_form_validate(&$form, FormStateInterface $form_state) {
if (empty($form_state
->getValue([
'fields',
'entity_print_view',
'empty_cell',
]))) {
$form_state
->setErrorByName('fields][entity_print_view][empty_cell', t('The Entity Print fields settings have not been saved. Please enter a non-empty value for the Entity Print field.'));
}
}
/**
* Entity display form submit handler.
*/
function entity_print_form_entity_view_display_edit_form_submit(&$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
$display = $form_state
->getFormObject()
->getEntity();
// If we've enabled the entity_print_view field then save the label for
// rendering later.
if ($display
->getComponent('entity_print_view')) {
$value = $form_state
->getValue([
'fields',
'entity_print_view',
])['empty_cell'];
$display
->setThirdPartySetting('entity_print', 'label', $value);
$display
->save();
}
}
Functions
Name | Description |
---|---|
entity_print_entity_extra_field_info_alter | Implements hook_entity_extra_field_info_alter(). |
entity_print_entity_view_alter | Implements hook_entity_view_alter(). |
entity_print_form_entity_view_display_edit_form_alter | Implements hook_form_FORM_ID_alter(). |
entity_print_form_entity_view_display_edit_form_submit | Entity display form submit handler. |
entity_print_form_entity_view_display_edit_form_validate | Entity display form validation handler. |
entity_print_theme | Implements hook_theme(). |