Search code examples
typo3typo3-9.xpowermail

How to extend date picker viewhelper of EXT:powermail?


I need to add a custom validator to the datepicker field. By default, this field comes without any validators.

I've already made the validator settings visible in the TCA of tx_powermail_domain_model_field and added my custom validator as usual.

Now I need the attributes data-parsley-customXXX and data-parsley-error-message added to the HTML input field which is usually done via the the viewhelper in ValidationDataAttributeViewHelper.php:
https://github.com/einpraegsam/powermail/blob/develop/Classes/ViewHelpers/Validation/ValidationDataAttributeViewHelper.php#L342
https://github.com/einpraegsam/powermail/blob/develop/Classes/ViewHelpers/Validation/ValidationDataAttributeViewHelper.php#L348

This is the code I need to extend: https://github.com/einpraegsam/powermail/blob/develop/Classes/ViewHelpers/Validation/DatepickerDataAttributeViewHelper.php#L32


Solution

  • I found a solution for my problem. As suggested in the comment it's possible to extend the Viewhelper:

    ext_localconf.php:

    $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\In2code\Powermail\ViewHelpers\Validation\DatepickerDataAttributeViewHelper::class] = [
      'className' => \Vendor\MyExt\Powermail\ViewHelpers\Validation\DatepickerDataAttributeViewHelper::class
    ];
    

    myext/Classes/Powermail/ViewHelpers/Validation/DatepickerDataAttributeViewHelper.php

    <?php
    declare(strict_types=1);
    namespace Vendor\MyExt\Powermail\ViewHelpers\Validation;
    
    use In2code\Powermail\Domain\Model\Field;
    use In2code\Powermail\Utility\LocalizationUtility;
    
    class DatepickerDataAttributeViewHelper extends \In2code\Powermail\ViewHelpers\Validation\DatepickerDataAttributeViewHelper
    {
        /**
         * Returns Data Attribute Array Datepicker settings (FE + BE)
         *
         * @return array for data attributes
         */
        public function render(): array
        {
            /** @var Field $field */
            $field = $this->arguments['field'];
            $additionalAttributes = $this->arguments['additionalAttributes'];
            $value = $this->arguments['value'];
    
            $additionalAttributes['data-datepicker-force'] =
                $this->settings['misc']['datepicker']['forceJavaScriptDatePicker'];
            $additionalAttributes['data-datepicker-settings'] = $this->getDatepickerSettings($field);
            $additionalAttributes['data-datepicker-months'] = $this->getMonthNames();
            $additionalAttributes['data-datepicker-days'] = $this->getDayNames();
            $additionalAttributes['data-datepicker-format'] = $this->getFormat($field);
            if ($value) {
                $additionalAttributes['data-date-value'] = $value;
            }
    
            if ($field->getValidation() && $this->isClientValidationEnabled()) {
                $value = 1;
                if ($field->getValidationConfiguration()) {
                    $value = $field->getValidationConfiguration();
                }
                $additionalAttributes['data-parsley-custom' . $field->getValidation()] = $value;
                $additionalAttributes['data-parsley-error-message'] =
                    LocalizationUtility::translate('validationerror_validation.' . $field->getValidation());
            }
    
            $this->addMandatoryAttributes($additionalAttributes, $field);
    
            return $additionalAttributes;
        }
    }