skip to Main Content

Use case: When saving a news istance in backend I want to manipulate values before saving to database.

  • I include my hook in the following way in ext_localconf.php:

$GLOBALS[‘TYPO3_CONF_VARS’][‘SC_OPTIONS’][‘t3lib/class.t3lib_tcemain.php’][‘processDatamapClass’][]= VendormythemeHooksNewsControllerSettings::class;

  • In my Hooks I have write this code to Check if the Hook is Firing but it haven’t .

    <?php
    
     namespace VendormythemeHooks;
    
      class NewsControllerSettings {
    
              public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray,TYPO3CMSCoreDataHandlingDataHandler $pObj){
                  echo 'Hello world ! '; 
                  exit();
              }
     }
    

I can’t upgrade the version of typo3 or even the news, but I don’t know what else to try.
I hope someone can help me. Opinions are also welcome.
Thanks in advance

2

Answers


  1. I think you don’t have the right signature for your method, parameter &$fieldArray, try this :

    <?php
    
    namespace VendormythemeHooks;
    
    class NewsControllerSettings {
    
      /**
       * Generate a different preview link     *
       *
       * @param string $status status
       * @param string $table table name
       * @param int $recordUid id of the record
       * @param array $fields fieldArray
       * @param TYPO3CMSCoreDataHandlingDataHandler $parentObject parent Object
       */
      public function processDatamap_afterDatabaseOperations(
          $status,
          $table,
          $recordUid,
          array $fields,
          TYPO3CMSCoreDataHandlingDataHandler $parentObject
      ) {
          echo 'Hello world ! '; 
          exit();
      }
    }
    
    Login or Signup to reply.
  2. First, register the hook in the ext_localconf.php like so:

    <?php
    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamap_afterDatabaseOperations'][] = VendorMythemeHooksDataHandlerHook::class . '->processDatamapAfterDatabaseOperations';
    

    Second, rename your namespace, as it doesn’t follow naming convention:

    <?php
    
    namespace VendorMythemeHooks;
    

    Third, make sure your filestructure is accurate:

    <pre>
    typo3conf/ext/mytheme/
    ├── Classes/
        └── Hooks/
            └── DataHandlerHook.php
    </pre>
    

    Fourth, fix your PHP class and give the file a descriptive name like DataHandlerHook.php:

    <?php
    
    namespace VendorMythemeHooks;
    
    class DataHandlerHook
    {
        /**
         * Hook function that is called after database operations are performed
         *
         * @param string $status The operation status ('new' or 'update')
         * @param string $table The table name being processed
         * @param int|string $id The record's ID
         * @param array $fieldArray The data array being processed
         * @param TYPO3CMSCoreDataHandlingDataHandler $dataHandler The DataHandler object
         */
        public function processDatamapAfterDatabaseOperations($status, $table, $id, &$fieldArray, TYPO3CMSCoreDataHandlingDataHandler $dataHandler)
        {
          # do something
        }
    }
    

    Fifth, make sure your namespace is reflected in the extensions composer.json file:

    {
      "name": "vendor/mytheme",
      "description": "Your TYPO3 extension description.",
      "type": "typo3-cms-extension",
      "autoload": {
        "psr-4": {
          "Vendor\Mytheme\": "Classes/"
        }
      }
    }
    

    Lastly, empty your caches.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search