[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_newsfeeds/src/View/Feeds/ -> JsonapiView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_newsfeeds
   6   *
   7   * @copyright   (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Newsfeeds\Api\View\Feeds;
  12  
  13  use Joomla\CMS\Language\Multilanguage;
  14  use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
  15  use Joomla\Component\Newsfeeds\Api\Serializer\NewsfeedSerializer;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * The feeds view
  23   *
  24   * @since  4.0.0
  25   */
  26  class JsonapiView extends BaseApiView
  27  {
  28      /**
  29       * The fields to render item in the documents
  30       *
  31       * @var  array
  32       * @since  4.0.0
  33       */
  34      protected $fieldsToRenderItem = [
  35          'id',
  36          'category',
  37          'name',
  38          'alias',
  39          'link',
  40          'published',
  41          'numarticles',
  42          'cache_time',
  43          'checked_out',
  44          'checked_out_time',
  45          'ordering',
  46          'rtl',
  47          'access',
  48          'language',
  49          'params',
  50          'created',
  51          'created_by',
  52          'created_by_alias',
  53          'modified',
  54          'modified_by',
  55          'metakey',
  56          'metadesc',
  57          'metadata',
  58          'publish_up',
  59          'publish_down',
  60          'description',
  61          'version',
  62          'hits',
  63          'images',
  64          'tags',
  65      ];
  66  
  67      /**
  68       * The fields to render items in the documents
  69       *
  70       * @var  array
  71       * @since  4.0.0
  72       */
  73      protected $fieldsToRenderList = [
  74          'id',
  75          'name',
  76          'alias',
  77          'checked_out',
  78          'checked_out_time',
  79          'category',
  80          'numarticles',
  81          'cache_time',
  82          'created_by',
  83          'published',
  84          'access',
  85          'ordering',
  86          'language',
  87          'publish_up',
  88          'publish_down',
  89          'language_title',
  90          'language_image',
  91          'editor',
  92          'access_level',
  93          'category_title',
  94      ];
  95  
  96      /**
  97       * The relationships the item has
  98       *
  99       * @var    array
 100       * @since  4.0.0
 101       */
 102      protected $relationship = [
 103          'category',
 104          'created_by',
 105          'modified_by',
 106          'tags',
 107      ];
 108  
 109      /**
 110       * Constructor.
 111       *
 112       * @param   array  $config  A named configuration array for object construction.
 113       *                          contentType: the name (optional) of the content type to use for the serialization
 114       *
 115       * @since   4.0.0
 116       */
 117      public function __construct($config = [])
 118      {
 119          if (\array_key_exists('contentType', $config)) {
 120              $this->serializer = new NewsfeedSerializer($config['contentType']);
 121          }
 122  
 123          parent::__construct($config);
 124      }
 125  
 126      /**
 127       * Execute and display a template script.
 128       *
 129       * @param   object  $item  Item
 130       *
 131       * @return  string
 132       *
 133       * @since   4.0.0
 134       */
 135      public function displayItem($item = null)
 136      {
 137          if (Multilanguage::isEnabled()) {
 138              $this->fieldsToRenderItem[] = 'languageAssociations';
 139              $this->relationship[]       = 'languageAssociations';
 140          }
 141  
 142          return parent::displayItem();
 143      }
 144  
 145      /**
 146       * Prepare item before render.
 147       *
 148       * @param   object  $item  The model item
 149       *
 150       * @return  object
 151       *
 152       * @since   4.0.0
 153       */
 154      protected function prepareItem($item)
 155      {
 156          if (Multilanguage::isEnabled() && !empty($item->associations)) {
 157              $associations = [];
 158  
 159              foreach ($item->associations as $language => $association) {
 160                  $itemId = explode(':', $association)[0];
 161  
 162                  $associations[] = (object) [
 163                      'id'       => $itemId,
 164                      'language' => $language,
 165                  ];
 166              }
 167  
 168              $item->associations = $associations;
 169          }
 170  
 171          if (!empty($item->tags->tags)) {
 172              $tagsIds   = explode(',', $item->tags->tags);
 173              $tagsNames = $item->tagsHelper->getTagNames($tagsIds);
 174  
 175              $item->tags = array_combine($tagsIds, $tagsNames);
 176          } else {
 177              $item->tags = [];
 178          }
 179  
 180          return parent::prepareItem($item);
 181      }
 182  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer