1

I have a section called News with 2 locales: 'en' and 'nl'. For some News items I have both languages switched on and for some only 1 language. The screenshot shows an example of an entry with only 1 language switched on.

enter image description here

Is there a php method to check the Entry object which languages are switch on?

2
  • Possible duplicate? See craftcms.stackexchange.com/questions/2991/…
    – jlcharette
    Apr 13 '16 at 18:41
  • The link to the other question could be a solution but doesn't seem to be a very neat one. At the moment I can get the enabled locales from a hook in the CMS section like this: craft()->templates->hook('cp.entries.edit.right-pane', function(&$context) { $locales = $context['enabledLocales']} Is there another way to get these enabledLocales straight from an entry object?
    – SiteHopper
    Apr 14 '16 at 9:43
3

After doing a quick search through the Craft app folder I found this method:

craft()->elements->getEnabledLocalesForElement($variables['entry']->id)

You can probably use this and send the EntryModel's id as the parameter

1
  • That worked perfectly well! Thank @Joris!
    – SiteHopper
    Apr 15 '16 at 10:26
1

Needed this for Craft 3 and came up with the following solution. Not sure if it’s the best approach, but it works.

/**
 * Returns in which sites an entry has active content
 * @entryId {int}
 * @return {array}
 */
public function getEntryLocales($entryId)
{
    $supportedSites = Entry::find()->id($entryId)->one()->getSupportedSites();
    $locales = array();

    foreach ($supportedSites as $site) {
        $entry = Entry::find()->id($entryId)->siteId($site['siteId'])->one();

        if ($entry) {
            $locales[] = $entry->site->language;
        }
    }

    return $locales;
}

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.