Search code examples
objecttypo3tx-news

Retrieving an object in controller when set hidden in backend TYPO3


I am using the news extension for managing news message for my website. Some of the news items are disabled in the backend. A user has an url with the id of news object and it will trigger an action in my controller

I am trying to get my news object like this

$news = $this->newsRepository->findByUid($id);

This will return a NULL because it's disabled/hidden in the backend. When i switch it back to enable. It will return my object nicely.

I tried it with the following function in my newsRepository

public function findHiddenByUid($uid) {

            $query = $this->createQuery();
            $query->getQuerySettings()->setRespectSysLanguage(FALSE);   
            $query->getQuerySettings()->setRespectStoragePage(FALSE);
            $query->getQuerySettings()->setEnableFieldsToBeIgnored(array('disable')); 
            return $query
            ->matching(
            $query->equals('uid', $uid)
            )
                ->execute()
                ->getFirst();

 }

But this will also return a NULL .

Is the function wrong, am I missing some settings? I am using TYPO3 7.6


Solution

  • Check out the findByUid I am using in the news extension:

    public function findByUid($uid, $respectEnableFields = true)
    {
        $query = $this->createQuery();
        $query->getQuerySettings()->setRespectStoragePage(false);
        $query->getQuerySettings()->setRespectSysLanguage(false);
        $query->getQuerySettings()->setIgnoreEnableFields(!$respectEnableFields);
    
        return $query->matching(
            $query->logicalAnd(
                $query->equals('uid', $uid),
                $query->equals('deleted', 0)
            ))->execute()->getFirst();
    }
    

    By calling ->findByUid(123,false) will also return hidden objects.