|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\silverback_gutenberg\Plugin\GutenbergBlockMutator; |
| 4 | + |
| 5 | +use Drupal\silverback_gutenberg\Attribute\GutenbergBlockMutator; |
| 6 | +use Drupal\silverback_gutenberg\BlockMutator\EntityBlockMutatorBase; |
| 7 | +use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 8 | + |
| 9 | +#[GutenbergBlockMutator( |
| 10 | + id: "term_reference_block_mutator", |
| 11 | + label: new TranslatableMarkup("Term References to UUIDs and vice versa."), |
| 12 | +)] |
| 13 | +class TermReferenceBlockMutator extends EntityBlockMutatorBase { |
| 14 | + |
| 15 | + /** |
| 16 | + * {@inheritDoc} |
| 17 | + */ |
| 18 | + public string $entityTypeId = 'taxonomy_term'; |
| 19 | + |
| 20 | + /** |
| 21 | + * {@inheritDoc} |
| 22 | + */ |
| 23 | + public function applies(array $block): bool { |
| 24 | + // Skip the parent applies() check since we want to be more flexible |
| 25 | + if (empty($block['attrs'])) { |
| 26 | + return FALSE; |
| 27 | + } |
| 28 | + |
| 29 | + // Check if any attribute is registered as a term reference |
| 30 | + foreach ($block['attrs'] as $attrName => $value) { |
| 31 | + // Skip empty values |
| 32 | + if (empty($value)) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + // Check if attribute is marked as a term reference |
| 37 | + if ($this->isTermReferenceAttribute($attrName, $block, $value)) { |
| 38 | + // Store the current attribute being processed |
| 39 | + $this->gutenbergAttribute = $attrName; |
| 40 | + |
| 41 | + // Auto-detect if multiple values |
| 42 | + $this->isMultiple = is_array($value); |
| 43 | + |
| 44 | + return TRUE; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return FALSE; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Determine if an attribute is a term reference. |
| 53 | + * |
| 54 | + * @param string $attrName |
| 55 | + * The attribute name to check. |
| 56 | + * @param array $block |
| 57 | + * The block data. |
| 58 | + * @param mixed $value |
| 59 | + * The attribute value. |
| 60 | + * |
| 61 | + * @return bool |
| 62 | + * TRUE if the attribute is a term reference. |
| 63 | + */ |
| 64 | + protected function isTermReferenceAttribute(string $attrName, array $block, $value): bool { |
| 65 | + if (preg_match('/(Term|Terms|TermId)$/i', $attrName)) { |
| 66 | + // For single values |
| 67 | + if (!is_array($value)) { |
| 68 | + return $this->isValidTermIdentifier($value); |
| 69 | + } |
| 70 | + // For multiple values, check if all values are valid identifiers |
| 71 | + elseif (is_array($value) && !empty($value)) { |
| 72 | + return array_reduce($value, function ($carry, $item) { |
| 73 | + return $carry && $this->isValidTermIdentifier($item); |
| 74 | + }, TRUE); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Add other checks here if needed |
| 79 | + return FALSE; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Check if a value is a valid term identifier (numeric ID or UUID). |
| 84 | + * |
| 85 | + * @param mixed $value |
| 86 | + * The value to check. |
| 87 | + * |
| 88 | + * @return bool |
| 89 | + * TRUE if the value is a valid term identifier. |
| 90 | + */ |
| 91 | + protected function isValidTermIdentifier($value): bool { |
| 92 | + // Check if it's a numeric term ID |
| 93 | + if (is_numeric($value)) { |
| 94 | + return TRUE; |
| 95 | + } |
| 96 | + |
| 97 | + // Check if it's a UUID format |
| 98 | + if (is_string($value) && preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', $value)) { |
| 99 | + return TRUE; |
| 100 | + } |
| 101 | + |
| 102 | + return FALSE; |
| 103 | + } |
| 104 | +} |
0 commit comments