vendor/terminal42/contao-node/src/ContentElement/NodesContentElement.php line 63

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Node Bundle for Contao Open Source CMS.
  5.  *
  6.  * @copyright  Copyright (c) 2019, terminal42 gmbh
  7.  * @author     terminal42 <https://terminal42.ch>
  8.  * @license    MIT
  9.  */
  10. namespace Terminal42\NodeBundle\ContentElement;
  11. use Contao\BackendTemplate;
  12. use Contao\ContentElement;
  13. use Contao\StringUtil;
  14. use Contao\System;
  15. use Patchwork\Utf8;
  16. use Terminal42\NodeBundle\Model\NodeModel;
  17. class NodesContentElement extends ContentElement
  18. {
  19.     /**
  20.      * Template.
  21.      *
  22.      * @var string
  23.      */
  24.     protected $strTemplate 'ce_nodes';
  25.     /**
  26.      * @var array
  27.      */
  28.     protected $nodes;
  29.     /**
  30.      * Display a wildcard in the back end.
  31.      *
  32.      * @return string
  33.      */
  34.     public function generate()
  35.     {
  36.         if (=== \count($ids StringUtil::deserialize($this->objModel->nodestrue))) {
  37.             return '';
  38.         }
  39.         $ids array_map('intval'$ids);
  40.         // Check for potential circular reference
  41.         if ('tl_node' === $this->objModel->ptable && \in_array((int) $this->objModel->pid$idstrue)) {
  42.             if (TL_MODE === 'BE') {
  43.                 return sprintf('<strong class="tl_red">%s</strong>'$GLOBALS['TL_LANG']['ERR']['circularReference']);
  44.             }
  45.             return '';
  46.         }
  47.         // Display the backend wildcard
  48.         if (TL_MODE === 'BE') {
  49.             return static::generateBackendWildcard($this->arrData$ids);
  50.         }
  51.         $this->nodes System::getContainer()->get('terminal42_node.manager')->generateMultiple($ids);
  52.         if (=== \count($this->nodes)) {
  53.             return '';
  54.         }
  55.         return parent::generate();
  56.     }
  57.     /**
  58.      * Generate a wildcard in the backend.
  59.      */
  60.     public static function generateBackendWildcard(array $data, array $ids): string
  61.     {
  62.         $nodes = [];
  63.         $nodeModels NodeModel::findBy(
  64.             ['id IN ('.implode(','$ids).')''type=?'],
  65.             [NodeModel::TYPE_CONTENTimplode(','$ids)],
  66.             ['order' => 'FIND_IN_SET(`id`, ?)']
  67.         );
  68.         if (null !== $nodeModels) {
  69.             $router System::getContainer()->get('router');
  70.             /** @var NodeModel $nodeModel */
  71.             foreach ($nodeModels as $nodeModel) {
  72.                 $nodes[] = sprintf(
  73.                     '<a href="%s" class="tl_gray" target="_blank">%s (ID: %s)</a>',
  74.                     $router->generate('contao_backend', ['do' => 'nodes''table' => 'tl_content''id' => $nodeModel->id]),
  75.                     $nodeModel->name,
  76.                     $nodeModel->id
  77.                 );
  78.             }
  79.         }
  80.         $wildcard '### '.Utf8::strtoupper($GLOBALS['TL_LANG']['FMD'][$data['type']][0]).' ###';
  81.         // Add nodes
  82.         if (\count($nodes) > 0) {
  83.             $wildcard .= '<br><p>'.implode('<br>'$nodes).'</p>';
  84.         }
  85.         $template = new BackendTemplate('be_wildcard');
  86.         $template->wildcard $wildcard;
  87.         return $template->parse();
  88.     }
  89.     /**
  90.      * Generate the module.
  91.      */
  92.     protected function compile(): void
  93.     {
  94.         $this->Template->nodes $this->nodes;
  95.     }
  96. }