【问题标题】:How can I get the TYPO3 "appearance" media images by using "ViewHelper"?如何使用“ViewHelper”获取 TYPO3“外观”媒体图像?
【发布时间】:2017-09-05 12:36:53
【问题描述】:

感谢阅读和回答...

我的 TYPO3 页面内容中有一些 TYPO3 网格元素。 每个网格元素都有一个选项卡“外观”,我在其中定义了一个图像文件。 从tt_content到媒体图像的数据库关系在sys_file_reference

我的问题是如何使用 ViewHelper 和我的 gridelement 的 uid 在我的流体模板中获取此图像文件?

【问题讨论】:

    标签: typo3 viewhelper


    【解决方案1】:

    我编写了一个小 ViewHelper(在 7.6 上测试,但在 8.7 上不需要太多更改)来获取时事通讯布局的参考图像:

    <?php
      namespace Vendor\Extension\ViewHelpers;
    
      use TYPO3\CMS\Core\Log\LogManager;
      use TYPO3\CMS\Core\Resource\FileRepository;
      use TYPO3\CMS\Core\Utility\GeneralUtility;
      use TYPO3\CMS\Core\Resource\FileReference;
      use TYPO3\CMS\Core\Resource\Exception;
      use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
      use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
      use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
    
      class ImagesReferencesViewHelper extends AbstractViewHelper implements CompilableInterface
      {
    
        /**
         * Iterates through elements of $each and renders child nodes
         *
         * @param int     $uid       The ID of the element
         * @param string  $table     The Referenced table name
         * @param string  $fieldName The Referenced field name
         * @param string  $as        The name of the iteration variable
         * @param string  $key       The name of the variable to store the current array key
         * @param boolean $reverse   If enabled, the iterator will start with the last element and proceed reversely
         * @param string  $iteration The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)
         * @param string  $link      The name of the variable to store link
         *
         * @return string Rendered string
         * @api
         */
        public function render($uid, $table = 'tt_content', $fieldName = 'assets', $as, $key = '', $reverse = false, $iteration = null, $link = null)
        {
          return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
        }
    
        /**
         * @param array                                                     $arguments
         * @param \Closure                                                  $renderChildrenClosure
         * @param \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
         *
         * @return string
         * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
         */
        static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
        {
          $templateVariableContainer = $renderingContext->getTemplateVariableContainer();
          if ($arguments['uid'] === null || $arguments['table'] === null || $arguments['fieldName'] === null) {
            return '';
          }
    
          /** @var FileRepository $fileRepository */
          $fileRepository = GeneralUtility::makeInstance(FileRepository::class);
          $images         = array();
    
          try {
            $images = $fileRepository->findByRelation($arguments['table'], $arguments['fieldName'], $arguments['uid']);
          } catch (Exception $e) {
            /** @var \TYPO3\CMS\Core\Log\Logger $logger */
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger();
            $logger->warning('The file-reference with uid  "' . $arguments['uid'] . '" could not be found and won\'t be included in frontend output');
          }
    
          if (is_object($images) && !$images instanceof \Traversable) {
            throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('ImagesReferencesViewHelper only supports arrays and objects implementing \Traversable interface', 1248728393);
          }
    
          if ($arguments['reverse'] === true) {
            // array_reverse only supports arrays
            if (is_object($images)) {
              $images = iterator_to_array($images);
            }
            $images = array_reverse($images);
          }
          $iterationData = array(
            'index' => 0,
            'cycle' => 1,
            'total' => count($images)
          );
    
          $output = '';
          foreach ($images as $keyValue => $singleElement) {
            $templateVariableContainer->add($arguments['as'], $singleElement);
    
            /** @var FileReference $singleElement */
            if ($arguments['link'] !== null && $singleElement->getLink()) {
              $link = $singleElement->getLink();
              /** @var ContentObjectRenderer $contentObject */
              $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
              $newLink       = $contentObject->typoLink_URL(array('parameter' => $link));
              $templateVariableContainer->add($arguments['link'], $newLink);
            }
    
            if ($arguments['key'] !== '') {
              $templateVariableContainer->add($arguments['key'], $keyValue);
            }
            if ($arguments['iteration'] !== null) {
              $iterationData['isFirst'] = $iterationData['cycle'] === 1;
              $iterationData['isLast']  = $iterationData['cycle'] === $iterationData['total'];
              $iterationData['isEven']  = $iterationData['cycle'] % 2 === 0;
              $iterationData['isOdd']   = !$iterationData['isEven'];
              $templateVariableContainer->add($arguments['iteration'], $iterationData);
              $iterationData['index']++;
              $iterationData['cycle']++;
            }
            $output .= $renderChildrenClosure();
            $templateVariableContainer->remove($arguments['as']);
            if ($arguments['link'] !== null && $singleElement->getLink()) {
              $templateVariableContainer->remove($arguments['link']);
            }
            if ($arguments['key'] !== '') {
              $templateVariableContainer->remove($arguments['key']);
            }
            if ($arguments['iteration'] !== null) {
              $templateVariableContainer->remove($arguments['iteration']);
            }
          }
          return $output;
        }
      }
    

    【讨论】:

    • 太好了,这正是我所需要的!!
    • 很高兴听到,所以请接受答案,这样所有人都可以看到您的问题已得到解答;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2021-05-27
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    相关资源
    最近更新 更多