Repository URL to install this package:
|
Version:
2.0.2 ▾
|
<?php
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Render\MarkupTrait;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_theme().
*/
function custom_forms_default_fields_theme() {
return [
'custom_forms_image_upload' => [
'base hook' => 'file_managed_file'
],
'form_element__amount_value' => [
'base hook' => 'form_element'
],
'form_element__plain_text' => [
'base hook' => 'form_element'
],
'form_element__text_format' => [
'base hook' => 'form_element'
],
'form_element__number' => [
'base hook' => 'form_element'
],
'form_element__email' => [
'base hook' => 'form_element'
],
];
}
/**
* Implements hook_help().
*/
function custom_forms_default_fields_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.custom_forms_default_fields':
return 'Adds some default fields which will then be available to be added to custom forms.';
}
}
/**
* Implements hook_locale_translation_projects_alter().
*/
function custom_forms_default_fields_locale_translation_projects_alter(&$projects) {
$module_handler = \Drupal::service('module_handler');
$path = $module_handler->getModule('custom_forms_default_fields')->getPath();
$projects['custom_forms_default_fields']['info']['interface translation server pattern'] = $path.'/translations/%language.po';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function custom_forms_default_fields_form_custom_forms_settings_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// We add an option for choosing which image styles should be available for the image upload field.
$form['image_upload'] = [
'#type' => 'details',
'#open' => FALSE,
'#title' => t('Image upload settings'),
];
$config = \Drupal::config('custom_forms_default_fields.settings');
$image_upload_config = $config->get('image_upload');
$image_style_entities = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple();
$image_styles = [];
foreach ($image_style_entities as $image_style) {
$image_styles[$image_style->id()] = $image_style->label();
}
$form['image_upload']['image_upload_enabled_styles'] = [
'#type' => 'checkboxes',
'#title' => t('Enabled image styles'),
'#options' => $image_styles
];
if (!empty($image_upload_config['enabled_styles'])) {
$form['image_upload']['image_upload_enabled_styles']['#default_value'] = $image_upload_config['enabled_styles'];
}
// Add custom submit handler so we can save our settings.
$form['#submit'][] = '__default_fields_settings_submission_handler';
}
/**
* Implements hook_preprocess_HOOK().
*/
function custom_forms_default_fields_preprocess_custom_forms_image_upload(&$variables) {
$element = $variables['element'];
$variables['attributes']['class'][] = 'image-upload';
$variables['attributes']['class'][] = 'js-image-upload';
$enable_preview = TRUE;
// check if we are using a private filepath.
if (strpos($element['#upload_location'], 'private://') === 0) {
// Private file storage does not support image styles.
$enable_preview = FALSE;
}
if (!empty($element['fids']['#value'])) {
/** @var \Drupal\file\Entity\File $file */
$file = reset($element['#files']);
if ($enable_preview && !empty($element['#preview_image_style'])) {
// Hide the filename if it's defined.
if (isset($element['file_' . $file->id()]['filename'])) {
$variables['element']['file_' . $file->id()]['filename']['#attributes']['class'][] = 'visually-hidden';
}
// Show the preview of the uploaded image.
$variables['element']['file_' . $file->id()]['thumbnail'] = array(
'#theme' => 'image_style',
'#style_name' => $element['#preview_image_style'],
'#uri' => $file->getFileUri()
);
} else {
// If preview is not enabled, just show the filename.
$variables['element']['file_' . $file->id()]['filename'] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#attributes' => [
'class' => [
'file-name',
],
],
'#value' => $file->getFilename(),
'#weight' => -10,
];
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function custom_forms_default_fields_form_custom_form_fields_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form['#attached']['library'][] = 'custom_forms_default_fields/admin-field-styling';
}
function __default_fields_settings_submission_handler(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
/** @var \Drupal\Core\Config\Config $config */
$config = \Drupal::service('config.factory')->getEditable('custom_forms_default_fields.settings');
$values = $form_state->cleanValues()->getValues();
if (!empty($values['image_upload_enabled_styles'])) {
$image_upload_config = [
'enabled_styles' => $values['image_upload_enabled_styles'],
];
$config->set('image_upload', $image_upload_config);
$config->save();
}
}