1

I'd like to get a field value in 3 differents locales in the same element-api endpoint but I can't make a request similar to this one : craft.entries.siteId($siteId).id($entryId).one() in twig.

<?php

use Craft;
use craft\elements\Entry;

return [
  'endpoints' => [
    'exhibitions.json' => function() {
      return [
        'elementType' => Entry::class,
        'criteria' => [
          'section' => 'exhibitions',
          // 'locale' => ['francais','english', 'deutsch']
        ],
        'transformer' => function(Entry $entry) {

          // -- Description --
          $description_fr = ($entry->summary) ? $entry->summary : ($entry->body) ? $entry->body : '';
          // $entry_en = $craft->entries->siteId(2).id($entry->id).one();
          // $description_en = ($entry_en->summary) ? $entry_en->summary : ($entry_en->body) ? $entry_en->body : '';
          // $entry_de = $craft->entries->siteId(3).id($entry->id).one();
          // $description_de = ($entry_de->summary) ? $entry_de->summary : ($entry_de->body) ? $entry_de->body : '';

          // -- Tags --
          $tags = '['.$entry->exhibitionsType->label.']';
          foreach ($entry->exhibitionsCats as $key => $cat) {
            $tags .= ',['.$cat['title'].']';
          }

          return [
            'type' => [
              array('target_id' => 'event')
            ],
            'title' => [
              array('value' => $entry->title)
            ],
            'field_subtitle' => [
              array('value' => $entry->subtitle)
            ],
            'field_description_fr' => [
              array('value' => $description_fr)
            ],
            // 'field_description_en' => [
            //   array('value' => $description_en)
            // ],
            // 'field_description_de' => [
            //   array('value' => $description_de)
            // ],
            'pictures' => [
              'url' => $entry->banner->one()->url,
              'copy' => $entry->banner->one()->legend,
              'alt' => $entry->banner->one()->legend
            ],
            'field_event_type' => [
              array('value' => '3') // Long or permanent event (3)
            ],
            'field_date' => [
              array(
                'value '=> $entry->fromDate->format('Y-m-dTH:i:s'),
                'end_value '=> $entry->toDate->format('Y-m-dTH:i:s'),
                'rrule '=> null,
              )
            ],
            'field_state' => [
              array('value' => 'scheduled')
            ],
            'field_type' => [
              array('target_id' => 6) // Audience - Pour tout public (6)
            ],
            'field_categories' => [
              array('target_id' => 24347) // Exhibitions (24347)
            ],
            'field_tags_rest' => [
              array('value' => $tags)
            ],
            'field_location' => [
              array('target_id' => 5179)
            ],
            'field_websites' => [
              array(
                'uri' => $entry->url,
                'title' => $entry->title)
            ],
            'field_email_address' => [
              array('value' => getenv('EIL_EMAIL'),)
            ],
          ];
        },
      ];
    }
  ]
];
2

Okay I found a way to do it if it can help someone:

$description_fr = ($entry->summary) ? $entry->summary : ($entry->body) ? $entry->body : '';
$entry_en = Entry::find()->id($entry->id)->siteId(2)->one();
$description_en = ($entry_en['summary']) ? $entry_en['summary'] : ($entry_en['body']) ? $entry_en['body'] : '';
$entry_de = Entry::find()->id($entry->id)->siteId(3)->one();
$description_de = ($entry_de['summary']) ? $entry_de['summary'] : ($entry_de['body']) ? $entry_de['body'] : '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.