fieldry-1.0.x-dev/fieldry.module

fieldry.module
<?php

/**
 * @file
 * Core module file for the fieldry module.
 */

use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\fieldry\FieldItemRemovalElement;

/**
 * Implements hook_field_widget_third_party_settings_form().
 */
function fieldry_field_widget_third_party_settings_form(WidgetInterface $widget, FieldDefinitionInterface $field_definition, $form_nmode, $form, FormStateInterface $form_state): ?array {
  if (FieldItemRemovalElement::isApplicable($widget, $field_definition)) {
    return FieldItemRemovalElement::buildSettingsForm($widget);
  }
  return NULL;
}

/**
 * Implements hook_field_widget_form_alter().
 */
function fieldry_field_widget_multivalue_form_alter(&$element, FormStateInterface $form_state, $context): void {
  $widget = $context['widget'];
  $fieldDefinition = $context['items']->getFieldDefinition();

  // Apply a remove button to applicable field widgets, which are enabled.
  if (FieldItemRemovalElement::isApplicable($widget, $fieldDefinition) && FieldItemRemovalElement::isEnabled($widget)) {
    $fieldRemoval = new FieldItemRemovalElement($context['items']->getName(), get_class($widget));
    $fieldRemoval->addRemoveButtons($context['items'], $element);
  }
}

/**
 * Implements hook_field_formatter_third_party_settings_form().
 */
function fieldry_field_formatter_third_party_settings_form(FormatterInterface $formatter, FieldDefinitionInterface $field_definition, $display_mode, $form, FormStateInterface $form_state): array {
  $account = \Drupal::currentUser();

  /** @var \Drupal\fieldry\FieldryWrapperFormBuilder $wrapperFormBuilder */
  $wrapperFormBuilder = \Drupal::service('fieldry.wrapper.form_builder');
  return $wrapperFormBuilder->buildForm($form, $form_state, $formatter, $field_definition, $account);
}

/**
 * Implements hook_field_formatter_settings_summary_alter().
 */
function fieldry_field_formatter_settings_summary_alter(&$summary, $context): void {
  $plugin = $context['formatter'];
  $settings = $plugin->getThirdPartySettings('fieldry') + [
    'wrapper' => 'default',
  ];

  if ($settings['wrapper'] !== 'default') {
    $definition = \Drupal::service('plugin.manager.fieldry_wrapper')
      ->getDefinition($settings['wrapper']);

    $summary[] = t('Field wrapper: @type.', [
      '@type' => $definition['label'],
    ]);
  }
}

/**
 * Implements hook_theme().
 */
function fieldry_theme(array $existing, $type, $theme, $path): array {
  return [
    'fieldry_wrapper' => [
      'base hook' => 'field',
      'render element' => 'element',
      'pattern' => 'fieldry_wrapper__',
      'file' => 'fieldry.theme',
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK_alter() for field.
 *
 * Only gets called if field formatter is using the standard "#theme => field"
 * and is safe for us to alter the theme output.
 */
function fieldry_theme_suggestions_field_alter(array &$suggestions, array $variables, string $hook): void {
  try {
    $wrapper_manager = \Drupal::service('plugin.manager.fieldry_wrapper');

    if ($wrapper = $wrapper_manager->getByElement($variables['element'])) {
      $wrapper->themeSuggestions($suggestions, $variables);
    }
  }
  catch (ServiceNotFoundException | PluginNotFoundException $e) {
    // Field wrapper manager or plugin is missing, use default field theme.
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc