[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_content/src/View/Articles/ -> JsonapiView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_content
   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\Content\Api\View\Articles;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Multilanguage;
  15  use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
  16  use Joomla\CMS\Plugin\PluginHelper;
  17  use Joomla\Component\Content\Api\Helper\ContentHelper;
  18  use Joomla\Component\Content\Api\Serializer\ContentSerializer;
  19  use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
  20  use Joomla\Registry\Registry;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * The article view
  28   *
  29   * @since  4.0.0
  30   */
  31  class JsonapiView extends BaseApiView
  32  {
  33      /**
  34       * The fields to render item in the documents
  35       *
  36       * @var  array
  37       * @since  4.0.0
  38       */
  39      protected $fieldsToRenderItem = [
  40          'id',
  41          'typeAlias',
  42          'asset_id',
  43          'title',
  44          'text',
  45          'tags',
  46          'language',
  47          'state',
  48          'category',
  49          'images',
  50          'metakey',
  51          'metadesc',
  52          'metadata',
  53          'access',
  54          'featured',
  55          'alias',
  56          'note',
  57          'publish_up',
  58          'publish_down',
  59          'urls',
  60          'created',
  61          'created_by',
  62          'created_by_alias',
  63          'modified',
  64          'modified_by',
  65          'hits',
  66          'version',
  67          'featured_up',
  68          'featured_down',
  69      ];
  70  
  71      /**
  72       * The fields to render items in the documents
  73       *
  74       * @var  array
  75       * @since  4.0.0
  76       */
  77      protected $fieldsToRenderList = [
  78          'id',
  79          'typeAlias',
  80          'asset_id',
  81          'title',
  82          'text',
  83          'tags',
  84          'language',
  85          'state',
  86          'category',
  87          'images',
  88          'metakey',
  89          'metadesc',
  90          'metadata',
  91          'access',
  92          'featured',
  93          'alias',
  94          'note',
  95          'publish_up',
  96          'publish_down',
  97          'urls',
  98          'created',
  99          'created_by',
 100          'created_by_alias',
 101          'modified',
 102          'hits',
 103          'version',
 104          'featured_up',
 105          'featured_down',
 106      ];
 107  
 108      /**
 109       * The relationships the item has
 110       *
 111       * @var    array
 112       * @since  4.0.0
 113       */
 114      protected $relationship = [
 115          'category',
 116          'created_by',
 117          'tags',
 118      ];
 119  
 120      /**
 121       * Constructor.
 122       *
 123       * @param   array  $config  A named configuration array for object construction.
 124       *                          contentType: the name (optional) of the content type to use for the serialization
 125       *
 126       * @since   4.0.0
 127       */
 128      public function __construct($config = [])
 129      {
 130          if (\array_key_exists('contentType', $config)) {
 131              $this->serializer = new ContentSerializer($config['contentType']);
 132          }
 133  
 134          parent::__construct($config);
 135      }
 136  
 137      /**
 138       * Execute and display a template script.
 139       *
 140       * @param   array|null  $items  Array of items
 141       *
 142       * @return  string
 143       *
 144       * @since   4.0.0
 145       */
 146      public function displayList(array $items = null)
 147      {
 148          foreach (FieldsHelper::getFields('com_content.article') as $field) {
 149              $this->fieldsToRenderList[] = $field->name;
 150          }
 151  
 152          return parent::displayList();
 153      }
 154  
 155      /**
 156       * Execute and display a template script.
 157       *
 158       * @param   object  $item  Item
 159       *
 160       * @return  string
 161       *
 162       * @since   4.0.0
 163       */
 164      public function displayItem($item = null)
 165      {
 166          $this->relationship[] = 'modified_by';
 167  
 168          foreach (FieldsHelper::getFields('com_content.article') as $field) {
 169              $this->fieldsToRenderItem[] = $field->name;
 170          }
 171  
 172          if (Multilanguage::isEnabled()) {
 173              $this->fieldsToRenderItem[] = 'languageAssociations';
 174              $this->relationship[]       = 'languageAssociations';
 175          }
 176  
 177          return parent::displayItem();
 178      }
 179  
 180      /**
 181       * Prepare item before render.
 182       *
 183       * @param   object  $item  The model item
 184       *
 185       * @return  object
 186       *
 187       * @since   4.0.0
 188       */
 189      protected function prepareItem($item)
 190      {
 191          $item->text = $item->introtext . ' ' . $item->fulltext;
 192  
 193          // Process the content plugins.
 194          PluginHelper::importPlugin('content');
 195          Factory::getApplication()->triggerEvent('onContentPrepare', ['com_content.article', &$item, &$item->params]);
 196  
 197          foreach (FieldsHelper::getFields('com_content.article', $item, true) as $field) {
 198              $item->{$field->name} = $field->apivalue ?? $field->rawvalue;
 199          }
 200  
 201          if (Multilanguage::isEnabled() && !empty($item->associations)) {
 202              $associations = [];
 203  
 204              foreach ($item->associations as $language => $association) {
 205                  $itemId = explode(':', $association)[0];
 206  
 207                  $associations[] = (object) [
 208                      'id'       => $itemId,
 209                      'language' => $language,
 210                  ];
 211              }
 212  
 213              $item->associations = $associations;
 214          }
 215  
 216          if (!empty($item->tags->tags)) {
 217              $tagsIds   = explode(',', $item->tags->tags);
 218              $tagsNames = $item->tagsHelper->getTagNames($tagsIds);
 219  
 220              $item->tags = array_combine($tagsIds, $tagsNames);
 221          } else {
 222              $item->tags = [];
 223          }
 224  
 225          if (isset($item->images)) {
 226              $registry = new Registry($item->images);
 227              $item->images = $registry->toArray();
 228  
 229              if (!empty($item->images['image_intro'])) {
 230                  $item->images['image_intro'] = ContentHelper::resolve($item->images['image_intro']);
 231              }
 232  
 233              if (!empty($item->images['image_fulltext'])) {
 234                  $item->images['image_fulltext'] = ContentHelper::resolve($item->images['image_fulltext']);
 235              }
 236          }
 237  
 238          return parent::prepareItem($item);
 239      }
 240  }


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