entity_legal-4.0.x-dev/entity_legal.module
entity_legal.module
<?php
/**
* @file
* Entity Legal module.
*
* @longdesc
* Provides versioned legal forms to serve to users. By default only a message
* method is supplied for user prompting.
*/
declare(strict_types=1);
use Drupal\Core\Link;
use Drupal\entity_legal\EntityLegalViewsConfigUpdater;
use Drupal\user\UserInterface;
use Drupal\views\ViewEntityInterface;
define('ENTITY_LEGAL_DOCUMENT_ENTITY_NAME', 'entity_legal_document');
define('ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME', 'entity_legal_document_version');
define('ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME', 'entity_legal_document_acceptance');
/**
* Implements hook_page_attachments_alter().
*/
function entity_legal_page_attachments_alter(array &$attachments) {
$context = ['attachments' => &$attachments];
// Execute Popup method plugin.
\Drupal::service('plugin.manager.entity_legal')
->createInstance('popup')
->execute($context);
}
/**
* Implements template_preprocess_page().
*/
function entity_legal_preprocess_page(&$variable) {
// Execute Message method plugin.
\Drupal::service('plugin.manager.entity_legal')
->createInstance('message')
->execute();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function entity_legal_form_user_register_form_alter(&$form, &$form_state) {
$context = ['form' => &$form];
// Execute Profile Form method plugin.
\Drupal::service('plugin.manager.entity_legal')
->createInstance('form_link')
->execute($context);
// Execute Profile Form Embedded method plugin.
\Drupal::service('plugin.manager.entity_legal')
->createInstance('form_inline')
->execute($context);
}
/**
* Implements hook_entity_extra_field_info().
*/
function entity_legal_entity_extra_field_info() {
$extra = [];
$documents = \Drupal::entityTypeManager()
->getStorage(ENTITY_LEGAL_DOCUMENT_ENTITY_NAME)
->loadByProperties(['require_signup' => 1]);
/** @var \Drupal\entity_legal\EntityLegalDocumentInterface $document */
foreach ($documents as $document) {
$publishedVersion = $document->getPublishedVersion();
if ($publishedVersion) {
$extra['user']['user']['form']['legal_' . $document->id()] = [
'label' => $publishedVersion->label(),
'description' => t('Checkbox for accepting :link', [
':link' => Link::createFromRoute($document->label(), 'entity.entity_legal_document.edit_form'),
]),
'weight' => -4,
];
}
}
return $extra;
}
/**
* Implements hook_entity_type_alter().
*/
function entity_legal_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
// Add a translation handler for fields if the language module is enabled.
if (\Drupal::moduleHandler()->moduleExists('language')) {
$translation = $entity_types['entity_legal_document_version']->get('translation');
$translation['entity_legal_document_version'] = TRUE;
$entity_types['entity_legal_document_version']->set('translation', $translation);
}
}
/**
* Implements hook_entity_ENTITY_TYPE_delete().
*/
function entity_legal_user_delete(UserInterface $account) {
$acceptance_storage = \Drupal::entityTypeManager()->getStorage(ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME);
$aids = $acceptance_storage->getQuery()
->accessCheck(FALSE)
->condition('uid', $account->id())
->execute();
if ($aids) {
$acceptance_storage->delete($acceptance_storage->loadMultiple($aids));
}
}
/**
* Provides a helper function to facilitate the transition to serial IDs.
*
* @param string $name
* The legacy string IDs.
*
* @return int
* The serial ID.
*
* @todo Remove in entity_legal:5.0.0.
*/
function _entity_legal_convert_name_to_serial(string $name): int {
if (!preg_match('/_(\d+)$/', $name, $found)) {
throw new \LogicException("The passed name '$name' is invalid");
}
return (int) $found[1];
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function entity_legal_view_presave(ViewEntityInterface $view): void {
/** @var \Drupal\entity_legal\EntityLegalViewsConfigUpdater $config_updater */
$config_updater = \Drupal::classResolver(EntityLegalViewsConfigUpdater::class);
$config_updater->updateAll($view);
}
