more_fields-2.2.19/more_fields.module
more_fields.module
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
/**
* Update fields schema more_fields_icon_text et more_fields_accordion_field.
*/
function more_fields_update_8002() {
// On recupere tous les champs.
$field_config_ids = \Drupal::entityQuery('field_storage_config')->accessCheck(FALSE)->condition('status', 1)->execute();
$field_config_entities = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadMultipleOverrideFree($field_config_ids);
// On a un soucis avec les tables "field_deleted_data_*", elle cause parfois
// des erreurs, elle contient les données de la precedante mise à jours.
// On va les supprimer avant d'importer.
$database = \Drupal::database();
$query = $database->query("SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'field_deleted_data_%' or table_name LIKE 'field_deleted_revision_%' ");
// Récupérer les résultats de la requête.
$results = $query->fetchAll();
// Afficher les noms des tables.
foreach ($results as $table) {
if (str_contains($table->table_name, "field_deleted_data_") || str_contains($table->table_name, "field_deleted_revision_")) {
$database->schema()->dropTable($table->table_name);
\Drupal::logger('more_fields')->notice("La table '$table->table_name' a été supprimé ");
}
}
// on filtre les champs creer à partir de "more_fields_icon_text".
foreach ($field_config_entities as $field_config_entity) {
/**
*
* @var \Drupal\field\Entity\FieldStorageConfig $field_config_entity
*/
if ($field_config_entity->get('type') == 'more_fields_icon_text' || $field_config_entity->get('type') == 'more_fields_accordion_field') {
__more_fields_update_schema_value_to_text($field_config_entity->get('entity_type'), $field_config_entity->get('field_name'), 'value');
}
}
}
/**
* Proceduire:
* On met à jour le schema du champs, cela va etre valide pour les prochaines
* champs.
* Mais pour ce qui est des champs deja creer
* il faut les recuperer et faire la MAJ de chacun.
* change value => varchar to text
*
* implement hook_update_8001
*
* @see https://www.drupal.org/node/2554097
* @see https://www.drupal.org/docs/drupal-apis/update-api/introduction-to-update-api-for-drupal-8
* @see https://www.drupal.org/docs/7/api/schema-api/updating-tables-hook_update_n-functions
*/
function more_fields_update_8001() {
// On recupere tous les champs.
$field_config_ids = \Drupal::entityQuery('field_storage_config')->accessCheck(FALSE)->condition('status', 1)->execute();
$field_config_entities = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadMultipleOverrideFree($field_config_ids);
// On filtre les champs creer à partir de "more_fields_icon_text".
foreach ($field_config_entities as $field_config_entity) {
/**
*
* @var \Drupal\field\Entity\FieldStorageConfig $field_config_entity
*/
if ($field_config_entity->get('type') == 'more_fields_icon_text') {
__more_fields_update_schema_value_to_text($field_config_entity->get('entity_type'), $field_config_entity->get('field_name'), 'value');
}
}
}
/**
* Update value max_length from 50 to 250.
*
* implement hook_update_8001
*
* @see https://www.drupal.org/node/2554097
* @see https://www.drupal.org/docs/drupal-apis/update-api/introduction-to-update-api-for-drupal-8
*/
function __more_fields_update_schema_value_to_text($entity_type_id, $field_name, $property_name) {
// Retrieve existing field data.
$database = \Drupal::database();
$table = $entity_type_id . '__' . $field_name;
$datas = $database->select($table, 'et')->fields('et', [])->execute()->fetchAll(\PDO::FETCH_ASSOC);
/**
*
* @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $updateManager
*/
// Remove old definition field, ceci entrainne la suppresion de la table.
$updateManager = \Drupal::entityDefinitionUpdateManager();
$storagedef = $updateManager->getFieldStorageDefinition($field_name, $entity_type_id);
$updateManager->uninstallFieldStorageDefinition($storagedef);
// Load new definition field in code.
/**
*
* @var \Drupal\Core\Entity\EntityFieldManager $fieldManager
*/
$fieldManager = \Drupal::service('entity_field.manager');
// $fields = $fieldManager->getFieldStorageDefinitions($entity_type_id);
$updateManager->installFieldStorageDefinition($field_name, $entity_type_id, "more_fields", $storagedef);
// Restore entity data in the new schema.
foreach ($datas as $data) {
$database->insert($table)->fields($data)->execute();
}
}
/**
* implement hook_form_FORM_ID_alter.
*/
function more_fields_form_field_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
/**
*
* @var Drupal\field\Entity\FieldConfig $fieldConfig
*/
$fieldConfig = $form_state->getFormObject()->getEntity();
if ($fieldConfig->getType() == 'image') {
/**
* à inclure dans le tuto :
* https://www.drupal.org/docs/drupal-apis/form-api/form-render-elements
*/
$form['more_fields_ratio'] = [
'#type' => 'textfield',
'#title' => "Ratio",
// '#precision' => 10,
// '#scale' => 0.1,
'#description' => t('Les valeurs comprises entre 0.5 et 2.0'),
'#default_value' => $fieldConfig->getThirdPartySetting('more_fields', 'more_fields_ratio')
];
$form['#entity_builders'][] = '_more_fields_form_field_config_edit_form_submit';
}
}
function _more_fields_form_field_config_edit_form_submit($entity_type, \Drupal\field\Entity\FieldConfig $fieldConfig, &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$key = 'more_fields_ratio';
$more_fields_ratio = $form_state->getValue($key);
if (!empty($more_fields_ratio)) {
$fieldConfig->setThirdPartySetting('more_fields', $key, $more_fields_ratio);
return;
}
$fieldConfig->unsetThirdPartySetting('more_fields', $key);
}
function _more_fields_get_current_ratio(array $element, FormStateInterface $form_state) {
/**
*
* @var \Drupal\Core\Entity\ContentEntityFormInterface $ContentForm
*/
$ContentForm = $form_state->getFormObject();
/**
* On souhaite appliquer cela uniquement sur les entitées.
*/
if ($ContentForm instanceof \Drupal\Core\Entity\ContentEntityFormInterface) {
/**
*
* @var \Drupal\Core\Entity\ContentEntityInterface $content
*/
$content = $ContentForm->getEntity();
if ($content->hasField($element['#field_name'])) {
/**
*
* @var \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field_image
*/
$field_image = $content->get($element['#field_name']);
/**
* (
*
* @var \Drupal\field\Entity\FieldConfig $FieldConfig
*/
$FieldConfig = $field_image->getFieldDefinition();
if ($FieldConfig instanceof \Drupal\field\Entity\FieldConfig) {
return $FieldConfig->getThirdPartySetting('more_fields', 'more_fields_ratio');
}
}
}
return null;
}
/**
* Implment hook_field_widget_single_element_WIDGET_TYPE_form_alter
*
* @param array $element
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param array $context
*/
function more_fields_field_widget_single_element_image_image_form_alter(array &$element, FormStateInterface $form_state, array $context) {
$more_fields_ratio = _more_fields_get_current_ratio($element, $form_state);
if ($more_fields_ratio) {
$element['more_fields_ratio'] = [
'#type' => 'html_tag',
'#tag' => 'div',
[
'#type' => 'html_tag',
'#tag' => 'small',
'#value' => t('The image must respect a ratio of ' . $more_fields_ratio)
]
];
}
$element['#element_validate'][] = '_more_fields_field_widget_single_element_image_image_form_validate';
}
function _more_fields_field_widget_single_element_image_image_form_validate($element, FormStateInterface $form_state, $form) {
$button = $form_state->getTriggeringElement();
// \Stephane888\Debug\debugLog::kintDebugDrupal($button,
// '_more_fields_field_widget_single_element_image_image_form_validate',
// true);
$files = $element['#files'];
if (!empty($button['#name']) && !str_contains($button['#name'], 'remove_button') && $files) {
$more_fields_ratio = _more_fields_get_current_ratio($element, $form_state);
if ($more_fields_ratio) {
foreach ($files as $k => $file) {
/**
*
* @var \Drupal\file\Entity\File $file
*/
$uri = $file->getFileUri();
$infos = getimagesize($uri);
$width = !empty($infos[0]) ? $infos[0] : null;
$height = !empty($infos[1]) ? $infos[1] : null;
if ($width && $height) {
$img_ratio = $width / $height;
$ecart = $more_fields_ratio * 0.1;
$r_min = $more_fields_ratio - $ecart;
$r_max = $more_fields_ratio + $ecart;
if ($img_ratio < $r_min || $img_ratio > $r_max) {
$form_state->setError($element, t('Your image does not respect the ratio, (image ratio : ' . number_format($img_ratio, 5) . ')'));
unset($element['#files'][$k]);
break;
}
}
}
}
}
}
/**
* implement template_preprocess_file_upload_help
*
* @param array $variables
*/
function more_fields_preprocess_file_upload_help(&$variables) {
// dump($variables);
// $variables['descriptions'][]=
}
function more_fields_field_widget_single_element_form_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
// dump($element);
}
/**
* Implements hook_theme().
*/
function more_fields_theme() {
$hooks = [];
$hooks['more_field_mit_gallery_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_field_mit_gallery_formatter'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_experience_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_fields_experience_formatter'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_value_niveau_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_fields_value_niveau_formatter'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_value_niveau_formatter2'] = $hooks['more_fields_value_niveau_formatter'];
$hooks['more_fields_experience_formatter2'] = $hooks['more_fields_experience_formatter'];
$hooks['more_fields_experience_formatter3'] = [
'preprocess functions' => [
'template_preprocess_more_fields_experience_formatter3'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_experience_formatter4'] = $hooks['more_fields_experience_formatter3'];
$hooks['more_fields_icon_text'] = [
'preprocess functions' => [
'template_preprocess_more_fields_icon_text'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
/**
* --
*/
$hooks['more_fields_field_chart'] = [
'render element' => 'element'
];
/**
* --
*/
$hooks['more_fields_text_bg'] = [
'preprocess functions' => [
'template_preprocess_more_fields_text_bg'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_accordion_field_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_fields_accordion_field_formatter'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_accordion_field_tab_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_fields_accordion_field_tab_formatter'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_bef_checkboxes'] = [
'preprocess functions' => [
'template_preprocess_more_fields_bef_checkboxes'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
// more_fields_bef_radioselement.items
$hooks['more_fields_bef_radios'] = [
'preprocess functions' => [
'template_preprocess_more_fields_bef_radios'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
// gallery overlay
$hooks['more_field_gallery_overlay'] = [
'preprocess functions' => [
'template_preprocess_gallery_overlay'
],
'variables' => [
'elements' => [],
'image_attributes' => [],
'settings' => []
],
'file' => 'more_fields.theme.inc'
];
// hbk file
$hooks['more_field_file_image_video'] = [
'preprocess functions' => [
'template_preprocess_more_field_file_image_video'
],
'variables' => [
'main_slider_items' => [],
'main_slider_items_attributes' => [],
'main_slider_attributes' => [],
'swiperjs_options' => [],
'swipper_attributes_buttons_next' => NULL,
'swipper_attributes_buttons_prev' => NULL,
'swipper_attributes_paginations' => NULL,
//
'thumbs_slider_items' => [],
'thumbs_slider_items_attributes' => [],
'thumbs_slider_attributes' => [],
'thumbs_slider_settings' => [],
//
'thumbs_attributes_paginations' => NULL,
'thumbs_attributes_buttons_prev' => NULL,
'thumbs_attributes_buttons_next' => NULL,
//
'items_types' => [],
'videos_settings' => []
],
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_video_player_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_field_file_image_video'
],
'variables' => [
'items' => [],
'video_attributes' => []
]
];
$hooks['more_fields_video_player_with_type_formatter'] = [
'variables' => [
'items' => [],
'video_attributes' => [],
'all_settings' => [],
'settings' => null
]
];
$hooks['more_fields_thumb_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_fields_thumb_formatter'
],
'variables' => [
'items' => null,
'item_attributes' => null,
'url' => null
]
];
$hooks['more_fields_restrained_text_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_fields_restrained_text_formatter'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_flexible_text_long_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_fields_flexible_text_long_formatter'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_multirender_formatter'] = [
'preprocess functions' => [
'template_preprocess_more_fields_multirender_formatter'
],
'render element' => 'element',
'file' => 'more_fields.theme.inc'
];
$hooks['more_fields_links'] = [
'render element' => 'element'
];
$hooks['more_fields_video_player_formatter'] = [
'variables' => [
'items' => NULL,
'player_attributes' => NULL
]
];
$hooks['more_field_galleries_images_videos'] = [
'variables' => [
'items' => []
]
];
return $hooks;
}
/**
* Prepares variables for bef-links template.
*
* Default template: bef-links.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the exposed form element.
*/
function template_preprocess_more_fields_links(array &$variables) {
\Drupal::moduleHandler()->loadInclude('better_exposed_filters', 'module');
template_preprocess_bef_links($variables);
//
foreach ($variables['links'] as $k => $value) {
if (!empty($variables['element'][$k]['#title']))
$variables['element'][$k]['#title'] = Markup::create($value['#title']);
$variables['links'][$k]['#title'] = Markup::create($value['#title']);
}
// Remove "form-select"
if (!empty($variables['attributes']['class'])) {
if ($key = array_search("form-select", $variables['attributes']['class']))
unset($variables['attributes']['class'][$key]);
}
}
