View source
<?php
namespace Drupal\file\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\file\Element\ManagedFile;
use Drupal\file\Entity\File;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
class FileWidget extends WidgetBase {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ElementInfoManagerInterface $element_info) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->elementInfo = $element_info;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('element_info'));
}
public static function defaultSettings() {
return [
'progress_indicator' => 'throbber',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['progress_indicator'] = [
'#type' => 'radios',
'#title' => t('Progress indicator'),
'#options' => [
'throbber' => t('Throbber'),
'bar' => t('Bar with progress meter'),
],
'#default_value' => $this
->getSetting('progress_indicator'),
'#description' => t('The throbber display does not show the status of uploads but takes up less space. The progress bar is helpful for monitoring progress on large uploads.'),
'#weight' => 16,
'#access' => file_progress_implementation(),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$summary[] = t('Progress indicator: @progress_indicator', [
'@progress_indicator' => $this
->getSetting('progress_indicator'),
]);
return $summary;
}
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition
->getName();
$parents = $form['#parents'];
$field_state = static::getWidgetState($parents, $field_name, $form_state);
if (isset($field_state['items'])) {
$items
->setValue($field_state['items']);
}
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
switch ($cardinality) {
case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
$max = count($items);
$is_multiple = TRUE;
break;
default:
$max = $cardinality - 1;
$is_multiple = $cardinality > 1;
break;
}
$title = $this->fieldDefinition
->getLabel();
$description = $this
->getFilteredDescription();
$elements = [];
$delta = 0;
foreach ($items as $item) {
$element = [
'#title' => $title,
'#description' => $description,
];
$element = $this
->formSingleElement($items, $delta, $element, $form, $form_state);
if ($element) {
if ($is_multiple) {
$element['_weight'] = [
'#type' => 'weight',
'#title' => t('Weight for row @number', [
'@number' => $delta + 1,
]),
'#title_display' => 'invisible',
'#delta' => $max,
'#default_value' => $item->_weight ?: $delta,
'#weight' => 100,
];
}
$elements[$delta] = $element;
$delta++;
}
}
$empty_single_allowed = $cardinality == 1 && $delta == 0;
$empty_multiple_allowed = ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || $delta < $cardinality) && !$form_state
->isProgrammed();
if ($empty_single_allowed || $empty_multiple_allowed) {
$items
->appendItem();
$element = [
'#title' => $title,
'#description' => $description,
];
$element = $this
->formSingleElement($items, $delta, $element, $form, $form_state);
if ($element) {
$element['#required'] = $element['#required'] && $delta == 0;
$elements[$delta] = $element;
}
}
if ($is_multiple) {
$elements['#file_upload_delta'] = $delta;
$elements['#type'] = 'details';
$elements['#open'] = TRUE;
$elements['#theme'] = 'file_widget_multiple';
$elements['#theme_wrappers'] = [
'details',
];
$elements['#process'] = [
[
static::class,
'processMultiple',
],
];
$elements['#title'] = $title;
$elements['#description'] = $description;
$elements['#field_name'] = $field_name;
$elements['#language'] = $items
->getLangcode();
$field_settings = $this
->getFieldSettings() + [
'display_field' => NULL,
];
$elements['#display_field'] = (bool) $field_settings['display_field'];
$elements['#file_upload_title'] = t('Add a new file');
$elements['#file_upload_description'] = [
'#theme' => 'file_upload_help',
'#description' => '',
'#upload_validators' => $elements[0]['#upload_validators'],
'#cardinality' => $cardinality,
];
}
return $elements;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$field_settings = $this
->getFieldSettings();
$field_settings += [
'display_default' => NULL,
'display_field' => NULL,
'description_field' => NULL,
];
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$defaults = [
'fids' => [],
'display' => (bool) $field_settings['display_default'],
'description' => '',
];
$element_info = $this->elementInfo
->getInfo('managed_file');
$element += [
'#type' => 'managed_file',
'#upload_location' => $items[$delta]
->getUploadLocation(),
'#upload_validators' => $items[$delta]
->getUploadValidators(),
'#value_callback' => [
static::class,
'value',
],
'#process' => array_merge($element_info['#process'], [
[
static::class,
'process',
],
]),
'#progress_indicator' => $this
->getSetting('progress_indicator'),
'#extended' => TRUE,
'#field_name' => $this->fieldDefinition
->getName(),
'#entity_type' => $items
->getEntity()
->getEntityTypeId(),
'#display_field' => (bool) $field_settings['display_field'],
'#display_default' => $field_settings['display_default'],
'#description_field' => $field_settings['description_field'],
'#cardinality' => $cardinality,
];
$element['#weight'] = $delta;
if (!isset($items[$delta]->fids) && isset($items[$delta]->target_id)) {
$items[$delta]->fids = [
$items[$delta]->target_id,
];
}
$element['#default_value'] = $items[$delta]
->getValue() + $defaults;
$default_fids = $element['#extended'] ? $element['#default_value']['fids'] : $element['#default_value'];
if (empty($default_fids)) {
$file_upload_help = [
'#theme' => 'file_upload_help',
'#description' => $element['#description'],
'#upload_validators' => $element['#upload_validators'],
'#cardinality' => $cardinality,
];
$element['#description'] = \Drupal::service('renderer')
->renderPlain($file_upload_help);
$element['#multiple'] = $cardinality != 1 ? TRUE : FALSE;
if ($cardinality != 1 && $cardinality != -1) {
$element['#element_validate'] = [
[
static::class,
'validateMultipleCount',
],
];
}
}
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$new_values = [];
foreach ($values as &$value) {
foreach ($value['fids'] as $fid) {
$new_value = $value;
$new_value['target_id'] = $fid;
unset($new_value['fids']);
$new_values[] = $new_value;
}
}
return $new_values;
}
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
parent::extractFormValues($items, $form, $form_state);
$field_name = $this->fieldDefinition
->getName();
$field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
$field_state['items'] = $items
->getValue();
static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
}
public static function value($element, $input, FormStateInterface $form_state) {
if ($input) {
if (empty($input['display'])) {
$input['display'] = $element['#display_field'] ? 0 : 1;
}
}
$return = ManagedFile::valueCallback($element, $input, $form_state);
$return += [
'fids' => [],
'display' => 1,
'description' => '',
];
return $return;
}
public static function validateMultipleCount($element, FormStateInterface $form_state, $form) {
$values = NestedArray::getValue($form_state
->getValues(), $element['#parents']);
$array_parents = $element['#array_parents'];
array_pop($array_parents);
$previously_uploaded_count = count(Element::children(NestedArray::getValue($form, $array_parents))) - 1;
$field_storage_definitions = \Drupal::service('entity_field.manager')
->getFieldStorageDefinitions($element['#entity_type']);
$field_storage = $field_storage_definitions[$element['#field_name']];
$newly_uploaded_count = count($values['fids']);
$total_uploaded_count = $newly_uploaded_count + $previously_uploaded_count;
if ($total_uploaded_count > $field_storage
->getCardinality()) {
$keep = $newly_uploaded_count - $total_uploaded_count + $field_storage
->getCardinality();
$removed_files = array_slice($values['fids'], $keep);
$removed_names = [];
foreach ($removed_files as $fid) {
$file = File::load($fid);
$removed_names[] = $file
->getFilename();
}
$args = [
'%field' => $field_storage
->getName(),
'@max' => $field_storage
->getCardinality(),
'@count' => $total_uploaded_count,
'%list' => implode(', ', $removed_names),
];
$message = t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args);
\Drupal::messenger()
->addWarning($message);
$values['fids'] = array_slice($values['fids'], 0, $keep);
NestedArray::setValue($form_state
->getValues(), $element['#parents'], $values);
}
}
public static function process($element, FormStateInterface $form_state, $form) {
$item = $element['#value'];
$item['fids'] = $element['fids']['#value'];
if ($element['#display_field']) {
$element['display'] = [
'#type' => empty($item['fids']) ? 'hidden' : 'checkbox',
'#title' => t('Include file in display'),
'#attributes' => [
'class' => [
'file-display',
],
],
];
if (isset($item['display'])) {
$element['display']['#value'] = $item['display'] ? '1' : '';
}
else {
$element['display']['#value'] = $element['#display_default'];
}
}
else {
$element['display'] = [
'#type' => 'hidden',
'#value' => '1',
];
}
if ($element['#description_field'] && $item['fids']) {
$config = \Drupal::config('file.settings');
$element['description'] = [
'#type' => $config
->get('description.type'),
'#title' => t('Description'),
'#value' => $item['description'] ?? '',
'#maxlength' => $config
->get('description.length'),
'#description' => t('The description may be used as the label of the link to the file.'),
];
}
if ($element['#cardinality'] != 1) {
$parents = array_slice($element['#array_parents'], 0, -1);
$new_options = [
'query' => [
'element_parents' => implode('/', $parents),
],
];
$field_element = NestedArray::getValue($form, $parents);
$new_wrapper = $field_element['#id'] . '-ajax-wrapper';
foreach (Element::children($element) as $key) {
if (isset($element[$key]['#ajax'])) {
$element[$key]['#ajax']['options'] = $new_options;
$element[$key]['#ajax']['wrapper'] = $new_wrapper;
}
}
unset($element['#prefix'], $element['#suffix']);
}
foreach ([
'upload_button',
'remove_button',
] as $key) {
$element[$key]['#submit'][] = [
static::class,
'submit',
];
$element[$key]['#limit_validation_errors'] = [
array_slice($element['#parents'], 0, -1),
];
}
return $element;
}
public static function processMultiple($element, FormStateInterface $form_state, $form) {
$element_children = Element::children($element, TRUE);
$count = count($element_children);
if (!$form_state
->isRebuilding()) {
$count_items_before = 0;
foreach ($element_children as $children) {
if (!empty($element[$children]['#default_value']['fids'])) {
$count_items_before++;
}
}
$form_state
->set('file_upload_delta_initial', $count_items_before);
}
foreach ($element_children as $delta => $key) {
if ($key != $element['#file_upload_delta']) {
$description = static::getDescriptionFromElement($element[$key]);
$element[$key]['_weight'] = [
'#type' => 'weight',
'#title' => $description ? t('Weight for @title', [
'@title' => $description,
]) : t('Weight for new file'),
'#title_display' => 'invisible',
'#delta' => $count,
'#default_value' => $delta,
];
}
else {
$element[$key]['#title'] = $element['#title'];
$element[$key]['_weight'] = [
'#type' => 'hidden',
'#default_value' => $delta,
];
}
}
$element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
$element['#suffix'] = '</div>';
return $element;
}
protected static function getDescriptionFromElement($element) {
if (!empty($element['#default_value']['description'])) {
return $element['#default_value']['description'];
}
if (!empty($element['#default_value']['filename'])) {
return $element['#default_value']['filename'];
}
return FALSE;
}
public static function submit($form, FormStateInterface $form_state) {
$button = $form_state
->getTriggeringElement();
$parents = array_slice($button['#parents'], 0, -2);
NestedArray::setValue($form_state
->getUserInput(), $parents, NULL);
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
$submitted_values = NestedArray::getValue($form_state
->getValues(), array_slice($button['#parents'], 0, -2));
foreach ($submitted_values as $delta => $submitted_value) {
if (empty($submitted_value['fids'])) {
unset($submitted_values[$delta]);
}
}
$new_values = [];
foreach ($submitted_values as $delta => $submitted_value) {
if (is_array($submitted_value['fids'])) {
foreach ($submitted_value['fids'] as $fid) {
$new_value = $submitted_value;
$new_value['fids'] = [
$fid,
];
$new_values[] = $new_value;
}
}
else {
$new_value = $submitted_value;
}
}
$submitted_values = array_values($new_values);
NestedArray::setValue($form_state
->getValues(), array_slice($button['#parents'], 0, -2), $submitted_values);
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items'] = $submitted_values;
static::setWidgetState($parents, $field_name, $form_state, $field_state);
}
public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
$clicked_button = end($form_state
->getTriggeringElement()['#parents']);
if ($clicked_button !== 'remove_button') {
parent::flagErrors($items, $violations, $form, $form_state);
}
}
}