0

I updated my Typo3 installation from version 11 to 12. I have an extension that extends the New System and I created an Author model class News extends \GeorgRinger\News\Domain\Model\NewsDefault Later, I get the UID from this model in the demand.

class NewsDemand extends \GeorgRinger\News\Domain\Model\Dto\NewsDemand
{
    /**
     * @var \BB\BbExtendNews\Domain\Model\Authors
     */
    protected $newsAuthor;

    /**
     * Set news author
     *
     * @param \BB\BbExtendNews\Domain\Model\Authors $newsAuthor
     * @return NewsDemand
     */
    public function setNewsAuthor($newsAuthor)
    {
        $this->newsAuthor = $newsAuthor;
        return $this;
    }

    /**
     * Get news author
     *
     * @return \BB\BbExtendNews\Domain\Model\Authors
     */
    public function getNewsAuthor()
    {
        return $this->newsAuthor;
    }
}

In debug mode, the variable exists, but the value is not being passed. How can I access/get this value in demand?


Extbase Variable Dump

GeorgRinger\News\Domain\Model\Dto\NewsDemandprototypetransient entity
   test => protected'' (0 chars)
   categories => protectedarray(5 items)
   categoryConjunction => protected'or' (2 chars)
   includeSubCategories => protectedFALSE
   author => protected'' (0 chars)
   tags => protected'' (0 chars)
   archiveRestriction => protected'' (0 chars)
   timeRestriction => protected'' (0 chars)
   timeRestrictionHigh => protected'' (0 chars)
   topNewsRestriction => protected0 (integer)
   dateField => protected'datetime' (8 chars)
   month => protected0 (integer)
   year => protected0 (integer)
   day => protected0 (integer)
   searchFields => protected'' (0 chars)
   search => protectedNULL
   order => protected'datetime desc' (13 chars)
   orderByAllowed => protected'sorting,author,uid,title,teaser,author,tstamp,crdate,datetime,categories.tit
      le' (78 chars)
   topNewsFirst => protectedFALSE
   storagePage => protected'1358' (4 chars)
   limit => protected0 (integer)
   offset => protected0 (integer)
   excludeAlreadyDisplayedNews => protectedFALSE
   hideIdList => protected'' (0 chars)
   idList => protected'' (0 chars)
   action => protected'BB\BbExtendNews\Controller\NewsController::listAuthorAction' (59 chars)
   class => protected'BB\BbExtendNews\Controller\NewsController' (41 chars)
   types => protectedarray(empty)
   _customSettings => protectedarray(empty)
   newsAuthor => protectedNULL
   uid => protectedNULL
   _localizedUid => protectedNULL
   _languageUid => protectedNULL
   _versionedUid => protectedNULL
   pid => protectedNULL

NewsController.php

class NewsController extends \GeorgRinger\News\Controller\NewsController {

    public function listAuthorAction(?array $overwriteDemand = null): ResponseInterface
    {
        $demand = $this->createDemandObjectFromSettings($this->settings);
        $demand->setActionAndClass(__METHOD__, self::class);

        if ((int)($this->settings['disableOverrideDemand'] ?? 1) !== 1 && $overwriteDemand !== null) {
            $demand = $this->overwriteDemandObject($demand, $overwriteDemand);
        }

   /*  It doesn't work */ if ($this->request->hasArgument('newsAuthor')) {
            $newsAuthor = (int)$this->request->getArgument('newsAuthor');
            $demand->setNewsAuthor($newsAuthor);
        } 

NewsRepository.php

        if ($demand->getNewsAuthor()) {
            $constraints['newsAuthor'] = $query->equals('newsAuthor', $demand->getNewsAuthor());
        }

With TYPO3 version 11 it all works I also created a simple variable (not an object), but that is not received in demand either. How can I access/get these values in demand? Any example or guidance on how to properly extend or modify Demand would be greatly appreciated.

1
  • The plugin namespace issue was the cause. This is a very common problem in TYPO3 Extbase extensions. Commented Aug 13 at 6:17

1 Answer 1

0

It’s working.

Why this happens:

TYPO3 Extbase uses plugin namespaces to separate parameters from different plugins.

Namespaces prevent parameter conflicts between different plugins on the same page.

  • The standard news plugin uses tx_news_pi1.

  • Your extended plugin could use tx_bbextendnews_pi1.

  • $this->request->getArgument() only looks in its own plugin namespace.

  • GeneralUtility::_GP() can query all namespaces.

A getNewsAuthorFromRequest function was created that searches for the value across all namespaces. In the listAuthorAction function, we retrieve this value into the demand object. Additionally, the controller was corrected according to TYPO3 v12 requirements.

  1. ResponseInterface:

  2. public function __construct()

<?php
namespace BB\BbExtendNews\Controller;

/***
 *
 * This file is part of the "Extend News" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 *  (c) 2018 Kay Röseler <[email protected]>, BippesBrandão GmbH
 *
 ***/

use GeorgRinger\News\Domain\Repository\CategoryRepository;
use GeorgRinger\News\Domain\Repository\TagRepository;
use GeorgRinger\News\Event\NewsListActionEvent;
use GeorgRinger\News\Utility\Cache;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use GeorgRinger\News\Domain\Repository\NewsRepository;


/**
 * Class NewsController
 * @package BB\BbExtendNews\Controller
 */
class NewsController extends \GeorgRinger\News\Controller\NewsController {

    /**
     * Output a list view of news
     *
     * @param array $overwriteDemand
     *
     * @throws \InvalidArgumentException
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
     * @throws \UnexpectedValueException
     */

    protected NewsRepository $newsRepository;

    protected CategoryRepository $categoryRepository;

    protected TagRepository $tagRepository;

    /** @var array */
    protected $ignoredSettingsForOverride = ['demandclass', 'orderbyallowed', 'selectedList'];

    /**
     * Original settings without any magic done by stdWrap and skipping empty values
     *
     * @var array
     */
    protected $originalSettings = [];

    public function __construct(
        NewsRepository $newsRepository,
        CategoryRepository $categoryRepository,
        TagRepository $tagRepository
    ) {
        $this->newsRepository = $newsRepository;
        $this->categoryRepository = $categoryRepository;
        $this->tagRepository = $tagRepository;
    }


    protected function getNewsAuthorFromRequest(): ?int
    {
        $newsAuthorUid = null;

  
        if (!$newsAuthorUid) {
            $namespacesToCheck = [
                'tx_news_pi1',
                'tx_bbextendnews_pi1',
                'tx_news',
                'tx_bbextendnews'
            ];

            foreach ($namespacesToCheck as $namespace) {
                $params = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP($namespace);
                if (is_array($params) && isset($params['newsAuthor'])) {
                    $newsAuthorUid = (int)$params['newsAuthor'];
                    \TYPO3\CMS\Core\Utility\DebugUtility::debug($newsAuthorUid, 'From Namespace: ' . $namespace);
                    break;
                }
            }
        }

        return $newsAuthorUid ?: null;
    }

    public function listAuthorAction(?array $overwriteDemand = null): ResponseInterface
    {
        $demand = $this->createDemandObjectFromSettings($this->settings);
        $demand->setActionAndClass(__METHOD__, __CLASS__);

        if ($this->settings['disableOverrideDemand'] != 1 && $overwriteDemand !== null) {
            $demand = $this->overwriteDemandObject($demand, $overwriteDemand);
        }

        $newsAuthorUid = $this->getNewsAuthorFromRequest();

       /* Typo3 12  It doesn't work ($this->request->hasArgument('newsAuthor')) {
            $newsAuthor = (int)$this->request->getArgument('newsAuthor');
            $demand->setNewsAuthor($newsAuthor);
        }*/

        if ($newsAuthorUid) {
            $demand->setNewsAuthor($newsAuthorUid);
          //  \TYPO3\CMS\Core\Utility\DebugUtility::debug($newsAuthorUid, 'Setting NewsAuthor UID');
        }

        // test
       // $newsAuthor = 4;
       // $demand->setNewsAuthor($newsAuthor);

        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($demand);

        $newsRecords = $this->newsRepository->findDemanded($demand);

       /* foreach ($newsRecords as $newsItem) {
            if ($newsItem->getNewsAuthor()) {
                $authorUid = $newsItem->getNewsAuthor()->_getProperty('uid');
                \TYPO3\CMS\Core\Utility\DebugUtility::debug($authorUid, 'Author UID');
            }
        }*/

        $assignedValues = [
            'news' => $newsRecords,
            'overwriteDemand' => $overwriteDemand,
            'demand' => $demand,
            'categories' => null,
            'tags' => null,
        ];

        if ($demand->getCategories() !== '') {
            $categoriesList = $demand->getCategories();
            if (!is_array($categoriesList)) {
                $categoriesList = GeneralUtility::trimExplode(',', $categoriesList);
            }
            if (!empty($categoriesList)) {
                $assignedValues['categories'] = $this->categoryRepository->findByIdList($categoriesList);
            }
        }

        if ($demand->getTags() !== '') {
            $tagList = $demand->getTags();
            if (!is_array($tagList)) {
                $tagList = GeneralUtility::trimExplode(',', $tagList);
            }
            if (!empty($tagList)) {
                $assignedValues['tags'] = $this->tagRepository->findByIdList($tagList);
            }
        }

        $event = $this->eventDispatcher->dispatch(new NewsListActionEvent($this, $assignedValues, $this->request));
        $this->view->assignMultiple($event->getAssignedValues());

        Cache::addPageCacheTagsByDemandObject($demand);
        return $this->htmlResponse();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?
I would strongly recommend to use a Hook or Event provided by the news extension to adjust the list by author view. Xclassing (like in this example) is not the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.