contextly-8.x-2.1/contextly.module
contextly.module
<?php
/**
* @file
* Contains contextly.module.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Implements hook_page_attachments().
*
* Add contextly meta tags to html head.
*/
function contextly_page_attachments(array &$page) {
$route_match = \Drupal::service('current_route_match');
if ($route_match->getRouteName() === 'entity.node.canonical' &&
$node = $route_match->getParameter('node')) {
/** @var \Drupal\contextly\ContextlyBaseServiceInterface $base_service */
$base_service = \Drupal::service('contextly.base');
if ($base_service->nodeContextlyIsDisabled($node)) {
// Node is excluded from contextly.
return;
}
$meta_generator = \Drupal::service('contextly.meta_generator');
$meta_generator->createMetaTags($node);
$contextly = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'name' => 'contextly-page',
'id' => 'contextly-page',
'content' => json_encode($meta_generator->getMetaTags()),
],
];
$page['#attached']['html_head'][] = [$contextly, 'contextly-page'];
}
}
/**
* Implements hook_preprocess_page().
*
* Add contextly libraries to page.
*/
function contextly_preprocess_page(array &$variables) {
if (\Drupal::routeMatch()->getRouteName() === 'entity.node.canonical' &&
!empty($variables['node'])) {
/** @var \Drupal\contextly\ContextlyBaseServiceInterface $base_service */
$base_service = \Drupal::service('contextly.base');
if (!$base_service->nodeContextlyIsDisabled($variables['node'])) {
$variables['#attached']['library'][] = 'contextly/contextly.head';
$variables['#attached']['library'][] = 'contextly/contextly.ready';
}
}
}
/**
* Implements hook_theme().
*/
function contextly_theme() {
return [
'contextly_node' => [
'template' => 'contextly-node',
],
'contextly_snippet_edit' => [
'template' => 'contextly-snippet-edit',
],
'contextly_node_edit' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_entity_base_field_info().
*/
function contextly_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() === 'node') {
$fields['contextly_disabled'] = _contextly_get_disabled_field_definition();
return $fields;
}
}
/**
* Return the base field definition for contextly disable field.
*
* @return \Drupal\Core\Field\BaseFieldDefinition
* The base field definition.
*/
function _contextly_get_disabled_field_definition() {
return BaseFieldDefinition::create('boolean')
->setLabel(t('Contextly disable'))
->setDescription(t('The contextly disable flag.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(FALSE);
}
/**
* Implements hook_form_FORM_ID_alter() for node_form().
*
* Adds edit elements to the node edit form.
*/
function contextly_form_node_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\EntityForm $form_object */
$form_object = $form_state->getFormObject();
/** @var \Drupal\node\NodeInterface $node */
$node = $form_object->getEntity();
if ($node->isNew()) {
// For now we support node edit only.
// Instead of showing the message now we add after build handler to check
// if the form was submitted and show the message if it wasn't. We can't do
// it here, because "process_input" form state variable is not set yet.
$form['#after_build'][] = '_contextly_create_node_form_after_build';
return;
}
$has_access = \Drupal::currentUser()->hasPermission('manage contextly links');
// Stop at this point if user has no access.
if (!$has_access) {
return;
}
/** @var \Drupal\contextly\ContextlyBaseServiceInterface $base_service */
$base_service = \Drupal::service('contextly.base');
if (!$base_service->isNodeTypeEnabled($node->bundle())) {
return;
}
// Make sure that API key is set.
$key = $base_service->getApiKey();
if (empty($key)) {
return;
}
// Add per-post settings (even if user has no access to edit links).
$form['contextly'] = [
'#type' => 'details',
'#group' => 'advanced',
'#attributes' => [
'class' => ['node-form-contextly'],
],
'#title' => t('Contextly widgets'),
'#weight' => 50,
'#attached' => [
'library' => ['contextly/node-form'],
],
];
$form['contextly']['contextly_disabled'] = [
'#type' => 'checkbox',
'#disabled' => !$has_access,
'#title' => t('Disable the recommendation module and all sidebars on this node'),
'#default_value' => !empty($node->contextly_disabled->value),
];
$form['contextly']['contextly_snippet_edit'] = [
'#theme' => 'contextly_node_edit',
];
$form['#attached']['library'][] = 'contextly/editor';
$form['#attached']['library'][] = 'contextly/editor-overlay';
$form['#attached']['drupalSettings'] = $base_service->getSettings($node);
}
/**
* Callback function.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*
* @return array
* The form array.
*/
function _contextly_create_node_form_after_build(array $form, FormStateInterface $form_state) {
if (!$form_state->isProgrammed() && !$form_state->isProcessingInput()) {
\Drupal::service('messenger')
->addWarning(t('Save the node to be able to manage the Contextly links.'));
}
return $form;
}
