Skip to content
Snippets Groups Projects
Commit 616ca5fb authored by David Galeano's avatar David Galeano
Browse files

Issue #3451941 by gxleano: Remove extra weight to the field type

parent e607a55e
No related branches found
No related tags found
1 merge request!10Issue #3451941 by gxleano: Remove extra weight to the field type
Pipeline #189204 passed
......@@ -205,9 +205,9 @@ class IconDialog extends FormBase {
// inside the parentheses.
// @see \Drupal\Core\Entity\Element\EntityAutocomplete::extractEntityIdFromAutocompleteInput
if (preg_match('/(.+\\s)\\(([^\\)]+)\\)/', $form_state->getValue('icon'), $matches)) {
$icon_short_name = trim($matches[1]);
$icon_name = trim($matches[1]);
$icon_collection = trim($matches[2]);
$icon_src = $this->iconify->getIconSource($icon_collection, $icon_short_name, $parameters);
$icon_src = $this->iconify->getIconSource($icon_collection, $icon_name, $parameters);
$values = [
'settings' => [
......
......@@ -81,7 +81,7 @@ class IconifyIconFormatter extends FormatterBase implements ContainerFactoryPlug
$settings = unserialize($icon['settings'] ?? '', ['allowed_classes']);
$icons[] = [
'#type' => 'iconify_icon',
'#icon' => $icon['icon_name'],
'#icon' => $icon['icon'],
'#settings' => $settings,
];
}
......
......@@ -30,22 +30,7 @@ class IconifyIcon extends FieldItemBase {
return [
// Columns contains the values that the field will store.
'columns' => [
'icon_name' => [
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
],
'icon_sort_name' => [
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
],
'icon_collection' => [
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
],
'icon_svg' => [
'icon' => [
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
......@@ -64,18 +49,9 @@ class IconifyIcon extends FieldItemBase {
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = [];
$properties['icon_name'] = DataDefinition::create('string')
->setLabel(t('Icon Name'))
$properties['icon'] = DataDefinition::create('string')
->setLabel(t('Icon'))
->setDescription(t('The full name of the icon'));
$properties['icon_sort_name'] = DataDefinition::create('string')
->setLabel(t('Icon Sort Name'))
->setDescription(t('The sort name of the icon'));
$properties['icon_collection'] = DataDefinition::create('string')
->setLabel(t('Icon Collection'))
->setDescription(t('The collection icon'));
$properties['icon_svg'] = DataDefinition::create('string')
->setLabel(t('Icon Svg'))
->setDescription(t('The svg icon'));
$properties['settings'] = DataDefinition::create('string')
->setLabel(t('Icon Settings'))
->setDescription(t('The additional class settings for the icon'));
......@@ -87,7 +63,7 @@ class IconifyIcon extends FieldItemBase {
* {@inheritdoc}
*/
public function isEmpty() {
$icon_name = $this->get('icon_name')->getValue();
$icon_name = $this->get('icon')->getValue();
return $icon_name === NULL || $icon_name === '';
}
......
......@@ -152,11 +152,17 @@ class IconifyIconWidget extends WidgetBase {
$selected_collections = $this->getSetting('collections');
$collection = implode(',', array_filter($selected_collections, static fn($value) => $value !== 0));
$settings = unserialize($iconify_icon->get('settings')->getValue() ?? '', ['allowed_classes']);
$icon = $iconify_icon->get('icon')->getValue() ?? '';
// Extract the icon name and collection name using a regular expression.
if ($iconDetails = $this->extractIconDetails($icon)) {
[$icon_name, $icon_collection] = $iconDetails;
$icon_svg = $this->iconify->generateSvg($icon_collection, $icon_name, $settings);
}
$element['icon_name'] = [
$element['icon'] = [
'#type' => 'iconify_icons',
'#title' => $cardinality === 1 ? $this->fieldDefinition->getLabel() : $this->t('Icon Name'),
'#default_value' => $iconify_icon->get('icon_name')->getValue(),
'#default_value' => $iconify_icon->get('icon')->getValue(),
'#required' => $element['#required'],
'#size' => 50,
'#attributes' => [
......@@ -167,8 +173,8 @@ class IconifyIconWidget extends WidgetBase {
'drupalSettings' => [
'iconify_icons' => [
$settings_key => [
'icon_name' => $iconify_icon->get('icon_name')->getValue(),
'icon_svg' => $iconify_icon->get('icon_svg')->getValue(),
'icon_name' => $icon,
'icon_svg' => $icon_svg ?? '',
],
],
],
......@@ -262,14 +268,6 @@ class IconifyIconWidget extends WidgetBase {
foreach ($values as $delta => &$item) {
$item['delta'] = $delta;
$item['selected_collection'] = $collection;
// Take 'Icon name (collection name)', match the collection name from
// inside the parentheses.
// @see \Drupal\Core\Entity\Element\EntityAutocomplete::extractEntityIdFromAutocompleteInput
if (preg_match('/(.+\\s)\\(([^\\)]+)\\)/', $item['icon_name'], $matches)) {
$item['icon_sort_name'] = trim($matches[1]);
$item['icon_collection'] = trim($matches[2]);
$item['icon_svg'] = $this->iconify->generateSvg($item['icon_collection'], $item['icon_sort_name'], $item['settings']);
}
$item['settings'] = serialize(array_filter($item['settings']));
}
......@@ -323,4 +321,22 @@ class IconifyIconWidget extends WidgetBase {
return $options;
}
/**
* Extracts icon name and collection name from the given string.
*
* @param string $icon
* The icon string in the format 'Icon name (collection name)'.
*
* @return array|null
* An array containing the icon name and collection name, or null if not
* matched.
*/
protected function extractIconDetails(string $icon): ?array {
if (preg_match('/(.+)\s\(([^)]+)\)/', $icon, $matches)) {
return [trim($matches[1]), trim($matches[2])];
}
return NULL;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment