diff --git a/src/Rulehelpers/GeneralHelper.php b/src/Rulehelpers/GeneralHelper.php index 89f7612b7fd598d460baa756a6d7091757ae9735..d54a7f486ee3e143b04fda003969b4744dc87e27 100644 --- a/src/Rulehelpers/GeneralHelper.php +++ b/src/Rulehelpers/GeneralHelper.php @@ -5,13 +5,13 @@ namespace Drupal\ai_interpolator\Rulehelpers; use Drupal\ai_interpolator\FormAlter\AiInterpolatorFieldConfig; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Utility\Token; use Drupal\file\FileInterface; -use Drupal\token\TreeBuilder; /** * Helper functions for most rules. @@ -55,6 +55,13 @@ class GeneralHelper { */ protected $currentUser; + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * Constructor for the class. * @@ -68,19 +75,23 @@ class GeneralHelper { * The token system. * @param \Drupal\Core\Session\AccountProxyInterface $currentUser * The current user. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The entity type manager. */ public function __construct( EntityFieldManagerInterface $entityFieldManager, ModuleHandlerInterface $moduleHandler, AiInterpolatorFieldConfig $aiInterpolatorFieldConfig, Token $token, - AccountProxyInterface $currentUser + AccountProxyInterface $currentUser, + EntityTypeManagerInterface $entityTypeManager ) { $this->entityFieldManager = $entityFieldManager; $this->moduleHandler = $moduleHandler; $this->aiInterpolatorFieldConfig = $aiInterpolatorFieldConfig; $this->token = $token; $this->currentUser = $currentUser; + $this->entityTypeManager = $entityTypeManager; } /** @@ -258,6 +269,78 @@ class GeneralHelper { return 'data:' . $imageEntity->getMimeType() . ';base64,' . base64_encode(file_get_contents($imageEntity->getFileUri())); } + /** + * Helper function to offer a form to joins multiple text values. + * + * @param string $id + * The id. + * @param array $form + * The form element, passed by reference. + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity + * @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition + * The field definition. + * @param array $extraJoiners + * Extra joiners specific to this field, assoc array with key/title. + * @param string $defaultJoiner + * The default joiner. + */ + public function addJoinerConfigurationFormField($id, array &$form, ContentEntityInterface $entity, FieldDefinitionInterface $fieldDefinition, array $extraJoiners = [], $defaultJoiner = "") { + $joiners = [ + '' => $this->t('-- Don\'t join --'), + ', ' => $this->t('Comma, with space (, )'), + ' ' => $this->t('Space ( )'), + '.' => $this->t('Period, with space (. )'), + '\n' => $this->t('New line (\n)'), + '\t' => $this->t('Tab (\t)'), + '<br />' => $this->t('HTML Break (<br />)'), + '<br /><br />' => $this->t('HTML Double Break (<br /><br />)'), + '<hr />' => $this->t('HTML Horizontal Rule (<hr />)'), + ',' => $this->t('Comma (,)'), + ';' => $this->t('Semicolon (;)'), + '.' => $this->t('Period (.)'), + 'other' => $this->t('Other'), + ]; + $joiners = array_merge_recursive($joiners, $extraJoiners); + $form["{$id}_joiner"] = [ + '#type' => 'select', + '#options' => $joiners, + '#title' => $this->t('Joiner'), + '#description' => $this->t('If you do not want multiple values back, this will take all values and join them.'), + '#default_value' => $fieldDefinition->getConfig($entity->bundle())->getThirdPartySetting('ai_interpolator', "{$id}_joiner", $defaultJoiner), + ]; + + $form["{$id}_joiner_other"] = [ + '#type' => 'textfield', + '#title' => $this->t('Other Joiner'), + '#description' => $this->t('If you selected other, please specify the joiner.'), + '#states' => [ + 'visible' => [ + 'select[name="' . $id .'_joiner"]' => [ + 'value' => 'other', + ], + ], + ], + ]; + } + + /** + * Helper function to join if wanted. + * + * @param array $values + * The values to join. + * @param string $joiner + * The joiner. + * + * @return string + * The joined string. + */ + public function joinValues(array $values, $joiner) { + // Make sure that newline and tab is evaluated. + $joiner = str_replace(['\n', '\t'], ["\n", "\t"], $joiner); + return implode($joiner, $values); + } + /** * Helper function to offer a form field as tokens from the entity. * @@ -352,4 +435,23 @@ class GeneralHelper { return $entityValue ?? $configValue; } + /** + * Get text format. + * + * @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition + * The field definition. + * + * @return string|null + * The format. + */ + public function getTextFormat(FieldDefinitionInterface $fieldDefinition) { + $allFormats = $this->entityTypeManager->getStorage('filter_format')->loadMultiple(); + // Maybe no formats are set. + if (empty($allFormats)) { + return NULL; + } + $format = $fieldDefinition->getSetting('allowed_formats'); + return $format[0] ?? key($allFormats); + } + }