Skip to content
Snippets Groups Projects
Commit b0a6ac57 authored by Oleh Vehera's avatar Oleh Vehera
Browse files

Issue #2718783 by mortona2k, voleger, dagomar, chOP, b_sharpe, Wouter...

Issue #2718783 by mortona2k, voleger, dagomar, chOP, b_sharpe, Wouter Waeytens, jhuhta, anand.toshniwal93, Yury N, pieter_duijves, guiumateu: Call to undefined method BaseFieldDefinition::getThirdPartySettings()
parent d41ba7bc
No related branches found
No related tags found
1 merge request!11Issue #2718783: Call to undefined method BaseFieldDefinition::getThirdPartySettings()
......@@ -6,7 +6,6 @@
*/
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
......@@ -15,6 +14,7 @@ use Drupal\Core\Language\Language;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
use Drupal\filefield_paths\Utility\FieldItem;
use Symfony\Component\HttpFoundation\Response;
// @TODO - Turn this into a plugin.
......@@ -212,13 +212,9 @@ function filefield_paths_form_field_config_edit_form_alter(array &$form, FormSta
function filefield_paths_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
// Force all File (Field) Paths uploads to go to the temporary file system
// prior to being processed.
if (isset($element['#type']) && $element['#type'] == 'managed_file') {
$settings = $context['items']->getFieldDefinition()
->getThirdPartySettings('filefield_paths');
if (isset($settings['enabled']) && $settings['enabled']) {
$element['#upload_location'] = \Drupal::config('filefield_paths.settings')
->get('temp_location');
}
if (FieldItem::hasConfigurationEnabled(FieldItem::getFromSupportedWidget($element, $context))) {
$element['#upload_location'] = \Drupal::config('filefield_paths.settings')
->get('temp_location');
}
}
......@@ -349,17 +345,8 @@ function filefield_paths_entity_update(EntityInterface $entity) {
}
$module_handler = \Drupal::moduleHandler();
foreach ($entity->getFields() as $field) {
if (!$field instanceof FileFieldItemList) {
continue;
}
/** @var \Drupal\field\FieldConfigInterface $definition */
$definition = $field->getFieldDefinition();
// Ignore base fields.
if (!$definition instanceof ThirdPartySettingsInterface) {
continue;
}
$settings = $definition->getThirdPartySettings('filefield_paths');
if (isset($settings['enabled']) && $settings['enabled'] && $module_handler->hasImplementations('filefield_paths_process_file')) {
if (FieldItem::hasConfigurationEnabled($field)) {
$settings = FieldItem::getConfiguration($field);
// Invoke hook_filefield_paths_process_file().
$module_handler->invokeAll(
'filefield_paths_process_file',
......
<?php
namespace Drupal\filefield_paths\Utility;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
/**
* Field Item Utility.
*/
final class FieldItem {
/**
* Get filefield_paths field settings.
*
* @param \Drupal\Core\Field\FieldItemListInterface $fieldItemList
* A field item to check for settings.
*
* @return array
* The filefield_paths settings for the field if set, else empty.
*/
public static function getConfiguration(FieldItemListInterface $fieldItemList): array {
$definition = $fieldItemList->getFieldDefinition();
if ($definition instanceof ThirdPartySettingsInterface) {
return $definition->getThirdPartySettings('filefield_paths');
}
return [];
}
/**
* Check if filefield_paths is enabled for a field item.
*
* @param \Drupal\Core\Field\FieldItemListInterface|mixed $field
* A field to check.
*
* @return bool
* State of filefield_path functionality for a given file field.
*/
public static function hasConfigurationEnabled($field): bool {
return $field instanceof FileFieldItemList &&
(self::getConfiguration($field)['enabled'] ?? FALSE);
}
/**
* Field widget helper.
*
* @param $element
* Widget element.
* @param $context
* Widget context.
*
* @return \Drupal\file\Plugin\Field\FieldType\FileFieldItemList|null
* Returns Field Item List instance. Null if widget type is not supported.
*/
public static function getFromSupportedWidget(array $element, array $context): ?FileFieldItemList {
if(isset($element['#type']) && $element['#type'] === 'managed_file') {
return $context['items'] ?? NULL;
}
return NULL;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment