a12s-1.0.0-beta7/modules/page_context/a12s_page_context.module
modules/page_context/a12s_page_context.module
<?php
/**
* @file
* Primary module hooks for A12s Page Context module.
*/
use Drupal\a12s_page_context\Entity\PageContextForm;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
// @todo add an alter hook on "a12s_page_context_form" deletion, in
// order to update all child records which use overrides. All direct children
// should have their "override" state removed and their values updated with
// the current values of the entity being deleted.
/**
* Alter the theme settings.
*
* @see hook_form_system_theme_settings_alter()
*/
function a12s_page_context_form_system_theme_settings_alter(array &$form, FormStateInterface $form_state): void {
if ($theme = $form_state->getBuildInfo()['args'][0] ?? '') {
$context = ['theme' => $theme];
PageContextForm::alterForm('theme', $context, $form, $form_state);
}
}
/**
* Implements hook_form_alter().
*/
function a12s_page_context_form_alter(array &$form, FormStateInterface $form_state, $form_id): void {
if ($form_state->getFormObject() instanceof EntityFormInterface) {
$entity = $form_state->getFormObject()->getEntity();
$pluginType = NULL;
$context = ['form_id' => $form_id];
if ($entity instanceof ContentEntityInterface) {
$pluginType = 'entity';
$context['entity'] = $entity;
}
elseif ($entity instanceof ConfigEntityInterface) {
if ($definition = \Drupal::entityTypeManager()->getDefinition($entity->getEntityTypeId())) {
$pluginType = 'entity_type';
$context['entity_type'] = $definition->getBundleOf();
$context['bundle'] = $entity->id();
}
}
if (isset($pluginType)) {
PageContextForm::alterForm($pluginType, $context, $form, $form_state);
}
}
}
