diff --git a/conditional_fields.services.yml b/conditional_fields.services.yml index 86fe65f5c78fbed13311c7189bcaf1703055b686..764a8791e73733cb42df1637c84d688a61bdd240 100644 --- a/conditional_fields.services.yml +++ b/conditional_fields.services.yml @@ -19,6 +19,7 @@ services: - '@plugin.manager.conditional_fields_handlers' - '@entity_display.repository' - '@language_manager' + - '@entity_type.manager' conditional_fields.element_alter_helper: class: Drupal\conditional_fields\ConditionalFieldsElementAlterHelper arguments: diff --git a/src/ConditionalFieldsFormHelper.php b/src/ConditionalFieldsFormHelper.php index 2a7d082ffc416a4dbab7a73b7d80e70ab9eb6e75..01358b26320819910189596a791919fc239685b4 100644 --- a/src/ConditionalFieldsFormHelper.php +++ b/src/ConditionalFieldsFormHelper.php @@ -6,6 +6,7 @@ use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityDisplayRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManager; @@ -18,46 +19,46 @@ use Drupal\field\Entity\FieldStorageConfig; class ConditionalFieldsFormHelper { /** - * The form array being modified. + * Array of effects for being applied to the conditional fields in this form. * * @var array */ - public $form; + public $effects; /** - * The state of the form being modified. + * A form element information manager. * - * @var \Drupal\Core\Form\FormStateInterface + * @var \Drupal\Core\Render\ElementInfoManager */ - public $formState; + protected $elementInfo; /** - * Array of effects for being applied to the conditional fields in this form. + * The entity display repository. * - * @var array + * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface */ - public $effects; + protected $entityDisplayRepository; /** - * A form element information manager. + * The entity type manager. * - * @var \Drupal\Core\Render\ElementInfoManager + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $elementInfo; + protected $entityTypeManager; /** - * A manager for conditional fields handlers. + * The form array being modified. * - * @var \Drupal\conditional_fields\ConditionalFieldsHandlersManager + * @var array */ - protected $type; + public $form; /** - * The entity display repository. + * The state of the form being modified. * - * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface + * @var \Drupal\Core\Form\FormStateInterface */ - protected $entityDisplayRepository; + public $formState; /** * The LanguageManager service. @@ -66,6 +67,13 @@ class ConditionalFieldsFormHelper { */ protected $languageManager; + /** + * A manager for conditional fields handlers. + * + * @var \Drupal\conditional_fields\ConditionalFieldsHandlersManager + */ + protected $type; + /** * ConditionalFieldsFormHelper constructor. * @@ -77,12 +85,21 @@ class ConditionalFieldsFormHelper { * The entity display repository. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. */ - public function __construct(ElementInfoManager $element_info, ConditionalFieldsHandlersManager $type, EntityDisplayRepositoryInterface $entity_display_repository, LanguageManager $language_manager) { + public function __construct( + ElementInfoManager $element_info, + ConditionalFieldsHandlersManager $type, + EntityDisplayRepositoryInterface $entity_display_repository, + LanguageManager $language_manager, + EntityTypeManagerInterface $entity_type_manager, + ) { $this->elementInfo = $element_info; $this->type = $type; $this->entityDisplayRepository = $entity_display_repository; $this->languageManager = $language_manager; + $this->entityTypeManager = $entity_type_manager; } /** @@ -343,8 +360,7 @@ class ConditionalFieldsFormHelper { $subform_exploded = explode('[subform][', $dependee_form_field['#name']); $first_field = \explode('[', $subform_exploded[0])[0]; $field_name = explode('][', end($subform_exploded)); - $storage = \Drupal::service('entity_type.manager') - ->getStorage('entity_form_display'); + $storage = $this->entityTypeManager->getStorage('entity_form_display'); $parent_widget_parents = $dependee_form_field['#array_parents']; while ($parent_widget_parents && end($parent_widget_parents) !== 'subform') { diff --git a/src/Form/ConditionalFieldDeleteForm.php b/src/Form/ConditionalFieldDeleteForm.php index 5d5eb996639d38e7575042c9e01ce04ac745c1e5..28124a90aa09c73e468f196ba7ba9cfdb62a6d2b 100644 --- a/src/Form/ConditionalFieldDeleteForm.php +++ b/src/Form/ConditionalFieldDeleteForm.php @@ -15,6 +15,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class ConditionalFieldDeleteForm extends ConfirmFormBase { + /** + * The bundle type this conditional field is attached to. + * + * @var string + */ + private $bundle; + /** * The entity type this conditional field is attached to. * @@ -23,11 +30,11 @@ class ConditionalFieldDeleteForm extends ConfirmFormBase { private $entityType; /** - * The bundle type this conditional field is attached to. + * The entity type manager. * - * @var string + * @var EntityTypeManagerInterface */ - private $bundle; + protected $entityTypeManager; /** * The name of the conditional field to delete. @@ -43,17 +50,13 @@ class ConditionalFieldDeleteForm extends ConfirmFormBase { */ private $uuid; - public function __construct( - protected EntityTypeManagerInterface $entityTypeManager, - ) {} - /** * {@inheritDoc} */ public static function create(ContainerInterface $container) { - return new static( - $container->get('entity_type.manager'), - ); + $instance = parent::create($container); + $instance->entityTypeManager = $container->get('entity_type.manager'); + return $instance; } /** diff --git a/src/Form/ConditionalFieldEditForm.php b/src/Form/ConditionalFieldEditForm.php index 64d7210c99190e24e854c55eac445aabfaf4efbe..22f6d1400d4ff0995bd1497d4dd4c1dc4380b947 100644 --- a/src/Form/ConditionalFieldEditForm.php +++ b/src/Form/ConditionalFieldEditForm.php @@ -4,17 +4,12 @@ namespace Drupal\conditional_fields\Form; use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Core\Datetime\DrupalDateTime; -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBase; -use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Render\Element; use Drupal\Core\Utility\Error; use Drupal\conditional_fields\ConditionalFieldsInterface; -use Drupal\conditional_fields\Conditions; use Drupal\conditional_fields\DependencyHelper; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -26,6 +21,41 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class ConditionalFieldEditForm extends FormBase { + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * The form builder. + * + * @var \Drupal\Core\Form\FormBuilderInterface + */ + protected $formBuilder; + + /** + * The list of conditions. + * + * @var \Drupal\conditional_fields\Conditions + */ + protected $list; + + /** + * The logger channel. + * + * @var \Drupal\Core\Logger\LoggerChannelInterface + */ + protected $logger; + + /** + * The module handler interface. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + /** * The name of the route to redirect to when the form has been submitted. * @@ -33,25 +63,17 @@ class ConditionalFieldEditForm extends FormBase { */ protected $redirectPath = 'conditional_fields.conditions_list'; - public function __construct( - protected Conditions $list, - protected EntityTypeManagerInterface $entityTypeManager, - protected FormBuilderInterface $formBuilder, - protected ModuleHandlerInterface $moduleHandler, - protected LoggerChannelInterface $logger, - ) {} - /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static( - $container->get('conditional_fields.conditions'), - $container->get('entity_type.manager'), - $container->get('form_builder'), - $container->get('module_handler'), - $container->get('logger.factory')->get('conditional_fields'), - ); + $instance = parent::create($container); + $instance->entityTypeManager = $container->get('entity_type.manager'); + $instance->formBuilder = $container->get('form_builder'); + $instance->list = $container->get('conditional_fields.conditions'); + $instance->logger = $container->get('logger.factory')->get('conditional_fields'); + $instance->moduleHandler = $container->get('module_handler'); + return $instance; } /** diff --git a/src/Form/ConditionalFieldForm.php b/src/Form/ConditionalFieldForm.php index 84ff74d4a3a45965b4758ca5baa59e8284fd1ba3..1c059834a67863d6a49c22dea55ad2ac057479d7 100644 --- a/src/Form/ConditionalFieldForm.php +++ b/src/Form/ConditionalFieldForm.php @@ -2,15 +2,10 @@ namespace Drupal\conditional_fields\Form; -use Drupal\Component\Uuid\UuidInterface; -use Drupal\Core\Entity\EntityFieldManagerInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; -use Drupal\conditional_fields\Conditions; use Drupal\conditional_fields\DependencyHelper; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -22,11 +17,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class ConditionalFieldForm extends FormBase { /** - * The route name for a conditional field edit form. + * Name of the entity bundle being configured. * * @var string */ - protected $editPath = 'conditional_fields.edit_form'; + protected $bundleName; /** * The route name for a conditional field delete form. @@ -35,6 +30,34 @@ class ConditionalFieldForm extends FormBase { */ protected $deletePath = 'conditional_fields.delete_form'; + /** + * The route name for a conditional field edit form. + * + * @var string + */ + protected $editPath = 'conditional_fields.edit_form'; + + /** + * The entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; + + /** + * Name of the entity type being configured. + * + * @var string + */ + protected $entityType; + + /** + * The Entity Type Manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * Form array. * @@ -50,38 +73,37 @@ class ConditionalFieldForm extends FormBase { protected $formState; /** - * Name of the entity type being configured. + * The list of conditions. * - * @var string + * @var \Drupal\conditional_fields\Conditions */ - protected $entityType; + protected $list; /** - * Name of the entity bundle being configured. + * The module handler. * - * @var string + * @var \Drupal\Core\Extension\ModuleHandlerInterface */ - protected $bundleName; + protected $moduleHandler; - public function __construct( - protected EntityFieldManagerInterface $entityFieldManager, - protected EntityTypeManagerInterface $entityTypeManager, - protected Conditions $list, - protected UuidInterface $uuidGenerator, - protected ModuleHandlerInterface $moduleHandler, - ) {} + /** + * The UUID generator. + * + * @var \Drupal\Component\Uuid\UuidInterface + */ + protected $uuidGenerator; /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static( - $container->get('entity_field.manager'), - $container->get('entity_type.manager'), - $container->get('conditional_fields.conditions'), - $container->get('uuid'), - $container->get('module_handler'), - ); + $instance = parent::create($container); + $instance->entityFieldManager = $container->get('entity_field.manager'); + $instance->entityTypeManager = $container->get('entity_type.manager'); + $instance->list = $container->get('conditional_fields.conditions'); + $instance->uuidGenerator = $container->get('uuid'); + $instance->moduleHandler = $container->get('module_handler'); + return $instance; } /** diff --git a/src/Plugin/conditional_fields/handler/TextAreaFormatted.php b/src/Plugin/conditional_fields/handler/TextAreaFormatted.php index 3eede57f1b70548f9cbe25e7db0d838914a38fcb..53356b3088a8a4b8d00f34307c11133b8a910ebe 100644 --- a/src/Plugin/conditional_fields/handler/TextAreaFormatted.php +++ b/src/Plugin/conditional_fields/handler/TextAreaFormatted.php @@ -32,20 +32,20 @@ class TextAreaFormatted extends ConditionalFieldsHandlerBase { break; case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND: - if (!empty($selectors)) { - foreach ($selectors as $selector) { - $input_states[$selector['selector']] = [ - $options['condition'] => $values_array, - ]; - $state[$options['state']][] = $input_states; - } - } - else { + // if (!empty($selectors)) { + // foreach ($selectors as $selector) { + // $input_states[$selector['selector']] = [ + // $options['condition'] => $values_array, + // ]; + // $state[$options['state']][] = $input_states; + // } + // } + // else { $input_states[$options['selector']] = [ $options['condition'] => $values_array, ]; $state[$options['state']] = $input_states; - } + // } break; case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX: diff --git a/tests/src/Unit/ConditionalFieldsFormHelperTest.php b/tests/src/Unit/ConditionalFieldsFormHelperTest.php index e54dbdf125cb54bd8588cd37a407a66ae371ff8e..6090f84b4154ae7439e8e282da0ca3df040cd586 100644 --- a/tests/src/Unit/ConditionalFieldsFormHelperTest.php +++ b/tests/src/Unit/ConditionalFieldsFormHelperTest.php @@ -9,6 +9,7 @@ use Drupal\Core\Render\ElementInfoManager; use Drupal\Tests\UnitTestCase; use Drupal\conditional_fields\ConditionalFieldsFormHelper; use Drupal\conditional_fields\ConditionalFieldsHandlersManager; +use Drupal\Core\Entity\EntityTypeManager; use Drupal\node\NodeForm; /** @@ -29,7 +30,8 @@ class ConditionalFieldsFormHelperTest extends UnitTestCase { $cfHandlersManager = $this->createMock(ConditionalFieldsHandlersManager::class); $entityDisplayRepository = $this->createMock(EntityDisplayRepositoryInterface::class); $languageManager = $this->createMock(LanguageManager::class); - $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager); + $entityTypeManager = $this->createMock(EntityTypeManager::class); + $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager, $entityTypeManager); // Set up fixtures. $sutClass->effects = []; @@ -55,7 +57,8 @@ class ConditionalFieldsFormHelperTest extends UnitTestCase { $cfHandlersManager = $this->createMock(ConditionalFieldsHandlersManager::class); $entityDisplayRepository = $this->createMock(EntityDisplayRepositoryInterface::class); $languageManager = $this->createMock(LanguageManager::class); - $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager); + $entityTypeManager = $this->createMock(EntityTypeManager::class); + $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager, $entityTypeManager); // Set up fixtures. $sutClass->effects = ['some_effect']; $sutClass->form = []; @@ -83,7 +86,8 @@ class ConditionalFieldsFormHelperTest extends UnitTestCase { $cfHandlersManager = $this->createMock(ConditionalFieldsHandlersManager::class); $entityDisplayRepository = $this->createMock(EntityDisplayRepositoryInterface::class); $languageManager = $this->createMock(LanguageManager::class); - $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager); + $entityTypeManager = $this->createMock(EntityTypeManager::class); + $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager, $entityTypeManager); // Set up fixtures. $formState = $this->createMock(FormState::class); @@ -177,7 +181,8 @@ class ConditionalFieldsFormHelperTest extends UnitTestCase { $cfHandlersManager = $this->createMock(ConditionalFieldsHandlersManager::class); $entityDisplayRepository = $this->createMock(EntityDisplayRepositoryInterface::class); $languageManager = $this->createMock(LanguageManager::class); - $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager); + $entityTypeManager = $this->createMock(EntityTypeManager::class); + $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager, $entityTypeManager); // Set up fixtures. $sutClass->form = ['#conditional_fields' => []]; @@ -204,7 +209,8 @@ class ConditionalFieldsFormHelperTest extends UnitTestCase { $cfHandlersManager = $this->createMock(ConditionalFieldsHandlersManager::class); $entityDisplayRepository = $this->createMock(EntityDisplayRepositoryInterface::class); $languageManager = $this->createMock(LanguageManager::class); - $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager); + $entityTypeManager = $this->createMock(EntityTypeManager::class); + $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager, $entityTypeManager); // Set up fixtures. $sutClass->form = ['#conditional_fields' => ['test' => 'test']]; @@ -232,7 +238,8 @@ class ConditionalFieldsFormHelperTest extends UnitTestCase { $cfHandlersManager = $this->createMock(ConditionalFieldsHandlersManager::class); $entityDisplayRepository = $this->createMock(EntityDisplayRepositoryInterface::class); $languageManager = $this->createMock(LanguageManager::class); - $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager); + $entityTypeManager = $this->createMock(EntityTypeManager::class); + $sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager, $entityDisplayRepository, $languageManager, $entityTypeManager); // Set up fixtures. $sutClass->form = ['#conditional_fields' => ['test' => 'test']];