ttv_assistant-1.0.x-dev/ttv_assistant.module
ttv_assistant.module
<?php
/**
* @file
* Contains hooks for the ttv_assistant module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
/**
* Implements hook_form_alter().
*
* Search from input text fields for avalable tags.
*/
function ttv_assistant_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_object = $form_state->getFormObject()) {
if (method_exists($form_object, 'getEntity')) {
$entity = $form_object->getEntity();
$entity_type = $entity->getEntityTypeId();
if ($entity_type == 'node') {
// Find taxonomy fields.
$bundle = $entity->bundle();
$entity_type = $entity->getEntityTypeId();
$entityFieldManager = \Drupal::service('entity_field.manager');
$fields = $entityFieldManager->getFieldDefinitions($entity_type, $bundle);
$text_fields = [];
$search_field_types = [
'text',
'text_long',
'text_with_summary',
'string',
'string_long',
'list_string',
];
$trigger_search_field = [];
$assistantService = \Drupal::service('ttv_assistant.assistant');
$taxonomy_fields = $assistantService::extractTaxonomyField($entity);
$search_fields = \Drupal::config('ttv_assistant.settings')->get('fields__' . $bundle);
if (!is_null($search_fields)) {
$trigger_search_field = array_filter($search_fields);
}
else {
if ($fields) {
foreach ($fields as $field_name => $field) {
// Get trigger fields for ajax call via .js.
if (in_array($field->getType(), $search_field_types)) {
$trigger_search_field[] = $field_name;
}
}
}
}
$assistant_settings = [
'bundle' => $bundle,
'trigger_fields' => $trigger_search_field,
'taxonomy_fields' => $taxonomy_fields,
'message' => \Drupal::config('ttv_assistant.settings')->get('found_message') ?? 'New tag found!',
];
$form['#attached']['drupalSettings']['assistant_settings'] = $assistant_settings;
}
}
}
}
/**
* Implements hook_theme().
*
* Twig template for render notification content.
*/
function ttv_assistant_theme($existing, $type, $theme, $path) {
return [
'assistant_widget' => [
'variables' => [
'uid' => NULL,
'node' => NULL,
'assistant_config' => NULL,
],
],
];
}
