View source
<?php
namespace Drupal\file_url\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\file\Element\ManagedFile;
use Drupal\file\Entity\File;
use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
use Drupal\file_url\Entity\RemoteFile;
use Drupal\file_url\FileUrlHandler;
class FileUrlWidget extends FileWidget {
const TYPE_UPLOAD = 'upload';
const TYPE_REMOTE = 'remote';
public static function defaultSettings() {
return [
'add_new_label' => 'Upload a new file or enter a URL',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
if ($this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality() !== 1) {
$element['add_new_label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label for new item form'),
'#default_value' => $this
->getSetting('add_new_label'),
];
}
return $element;
}
public function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
$elements = parent::formMultipleElements($items, $form, $form_state);
$elements['#file_upload_title'] = $this
->getSetting('add_new_label');
unset($elements['#file_upload_description']);
return $elements;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
if ($this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality() === 1) {
$element['#description'] = $this
->getFilteredDescription();
}
$element['#element_validate'] = [
[
static::class,
'validate',
],
];
return $element;
}
public static function process($element, FormStateInterface $form_state, $form) {
$file_url_type = isset($element['#value']['file_url_type']) ? $element['#value']['file_url_type'] : NULL;
$file_url_remote = $element['#value']['file_url_remote'];
$file_url_remote_is_valid = UrlHelper::isValid($file_url_remote, TRUE);
if ($file_url_remote_is_valid && $file_url_type) {
$remote_file = RemoteFile::load($file_url_remote);
$element['#files'] = [
$file_url_remote => $remote_file,
];
$file_link = [
'#type' => 'link',
'#title' => $remote_file
->getFileUri(),
'#url' => Url::fromUri($remote_file
->getFileUri()),
];
if ($element['#multiple']) {
$element["file_{$file_url_remote}"]['selected'] = [
'#type' => 'checkbox',
'#title' => \Drupal::service('renderer')
->renderPlain($file_link),
];
}
else {
$element["file_{$file_url_remote}"]['filename'] = $file_link + [
'#weight' => -10,
];
}
}
$access_file_url_elements = empty($element['#files']) && !$file_url_remote_is_valid || !$file_url_type;
$element['file_url_type'] = [
'#type' => 'radios',
'#options' => [
static::TYPE_UPLOAD => t('Upload file'),
static::TYPE_REMOTE => t('Remote file URL'),
],
'#default_value' => $file_url_type,
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'#access' => $access_file_url_elements,
'#weight' => 5,
];
$field_name = $element['#field_name'];
$delta = $element['#delta'];
$selector = ':input[name="' . $field_name . '[' . $delta . '][file_url_type]"]';
$remote_visible = [
$selector => [
'value' => static::TYPE_REMOTE,
],
];
$element['file_url_remote'] = [
'#type' => 'url',
'#title' => t('Remote URL'),
'#title_display' => 'invisible',
'#description' => t('This must be an external URL such as <em>http://example.com</em>.'),
'#default_value' => $file_url_remote,
'#states' => [
'visible' => $remote_visible,
],
'#attached' => [
'library' => [
'file_url/remote_url',
],
],
'#attributes' => [
'data-drupal-file-url-remote' => TRUE,
],
'#access' => $access_file_url_elements,
'#weight' => 15,
];
$upload_visible = [
$selector => [
'value' => static::TYPE_UPLOAD,
],
];
$element['upload']['#states']['visible'] = $upload_visible;
$element['upload']['#theme_wrappers'][] = 'form_element';
$element['upload']['#description'] = [
'#theme' => 'file_upload_help',
'#description' => '',
'#upload_validators' => $element['#upload_validators'],
'#cardinality' => $element['#cardinality'],
];
$element['upload']['#weight'] = 10;
$element['upload_button']['#weight'] = 20;
return parent::process($element, $form_state, $form);
}
public static function validate(&$element, FormStateInterface $form_state, &$complete_form) {
$file_url_type = NestedArray::getValue($form_state
->getValues(), array_merge($element['#parents'], [
'file_url_type',
]));
$remote_url = NestedArray::getValue($form_state
->getValues(), array_merge($element['#parents'], [
'file_url_remote',
]));
$fids = NestedArray::getValue($form_state
->getValues(), array_merge($element['#parents'], [
'fids',
]));
if (($remote_url || $fids) && !in_array($file_url_type, [
static::TYPE_UPLOAD,
static::TYPE_REMOTE,
], TRUE)) {
}
if ($file_url_type === static::TYPE_UPLOAD) {
ManagedFile::validateManagedFile($element, $form_state, $complete_form);
}
}
public static function value($element, $input, FormStateInterface $form_state) {
$file_url_remote = $file_url_type = NULL;
if (!empty($element['#default_value']['fids'])) {
$url = $element['#default_value']['fids'][0];
$file = FileUrlHandler::urlToFile($url);
if (!FileUrlHandler::isRemote($file)) {
$element['#default_value']['fids'] = [
$file
->id(),
];
$file_url_type = 'upload';
}
else {
$file_url_remote = $url;
$file_url_type = 'remote';
}
}
$return = parent::value($element, $input, $form_state);
if ($input !== FALSE) {
if (isset($input['file_url_type']) && $input['file_url_type'] === static::TYPE_REMOTE) {
if (!empty($input['file_url_remote']) && !$input['fids']) {
$file_url_remote = $input['file_url_remote'];
}
}
}
if (!empty($file_url_remote)) {
$return['fids'] = [
$file_url_remote,
];
}
if (empty($return['file_url_remote'])) {
$return['file_url_remote'] = $file_url_remote;
}
if (empty($return['file_url_type'])) {
$return['file_url_type'] = $file_url_type;
}
return $return;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$values = parent::massageFormValues($values, $form, $form_state);
foreach ($values as &$value) {
if ($value['file_url_type'] === static::TYPE_UPLOAD) {
$file = File::load((int) $value['target_id']);
$value['target_id'] = FileUrlHandler::fileToUrl($file);
}
unset($value['file_url_type']);
unset($value['file_url_remote']);
}
return $values;
}
}