taxonomy_place-8.x-1.3/taxonomy_place.module
taxonomy_place.module
<?php
/**
* @file
* Contains taxonomy_place.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\taxonomy\TermInterface;
/**
* Implements hook_help().
*/
function taxonomy_place_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.taxonomy_place':
$readme = __DIR__ . '/README.md';
$text = file_get_contents($readme);
$output = '';
// If the Markdown module is installed, use it to render the README.
if ($text && \Drupal::moduleHandler()->moduleExists('markdown') === TRUE) {
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
$output = $filter->process($text, 'en');
}
// Else the Markdown module is not installed output the README as text.
elseif ($text) {
$output = '<pre>' . $text . '</pre>';
}
// Add a link to the Drupal.org project.
$output .= '<p>';
$output .= t('Visit the <a href=":project_link">Taxonomy Place project page</a> on Drupal.org for more information.', [
':project_link' => 'https://www.drupal.org/project/taxonomy_place',
]);
$output .= '</p>';
return $output;
}
}
/**
* Implements hook_field_widget_form_alter().
*/
function taxonomy_place_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
if (!$form_state->getFormObject() instanceof EntityFormInterface) {
return;
}
$entity = $form_state->getFormObject()->getEntity();
$field_name = $context['items']->getName();
$config = \Drupal::service('config.factory')->get('taxonomy_place.settings');
$fields = $config->get('fields');
$labels = $config->get('labels');
if (!empty($labels)) {
foreach ($fields as $field) {
list(,$bundle, $address_field,) = explode(':', $field);
if ($entity->bundle() == $bundle && $field_name == $address_field) {
$element['#after_build'][] = 'taxonomy_place_address_after_build';
}
}
}
}
/**
* Custom address after_build, to alter the address field labels.
*/
function taxonomy_place_address_after_build($element, $form_state) {
$config = \Drupal::service('config.factory')->get('taxonomy_place.settings');
$labels = $config->get('labels');
foreach ($labels as $key => $label) {
$element['address'][$key]['#title'] = $label;
}
return $element;
}
/**
* Implements hook_entity_presave().
*/
function taxonomy_place_entity_presave(EntityInterface $entity) {
$config = \Drupal::service('config.factory')->get('taxonomy_place.settings');
$fields = $config->get('fields');
if (!empty($fields)) {
foreach ($fields as $field) {
list($entity_type, $bundle, $address_field, $entityreference_field) = explode(':', $field);
if ($entity_type == $entity->getEntityTypeId()) {
if ($bundle == $entity->bundle()) {
$values = [];
$address_values = $entity->get($address_field)->getValue();
$creator = \Drupal::service('taxonomy_place.creator');
foreach ($address_values as $delta => $address_value) {
if ($term = $creator->createPlaceTerm($entity, $address_field, $delta)) {
if ($term instanceof TermInterface) {
$values[$delta] = ['target_id' => $term->id()];
}
}
}
$entity->set($entityreference_field, $values);
}
}
}
}
}
