Group has a sub module -- Group language.


/**
 * Implements hook_language_negotiation_info().
 */
function group_language_language_negotiation_info() {
  $providers = array();

  $providers['group-language'] = array(
    'callbacks' => array('language' => 'group_language_from_group_context'),
    'weight' => -5,
    'name' => t('Group'),
    'description' => t("Follow the Group context's language preference."),
    'file' => drupal_get_path('module', 'group_language') .'/group_language.module',
  );

  return $providers;
}

/**
 * Identify language from Group context.
 *
 * @param $languages
 *   An array of valid language objects.
 *
 * @return
 *   A valid language code on success, FALSE otherwise.
 */
function group_language_from_group_context($languages) {
  // We need a full bootstrap in order to get the group context.
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

  if ($group = group_context()) {
  // ... Group' own logic.
  }
}

The drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
causes:

# Notice: Trying to get property of non-object in drupal_lookup_path()  (line 74 of /var/www/d7_dev/includes/path.inc).
# Notice: Trying to get property of non-object in entity_get_info() (line 6359 of /var/www/d7_dev/includes/common.inc).
# Notice: Trying to get property of non-object in entity_get_info() (line 6359 of /var/www/d7_dev/includes/common.inc).
# Notice: Trying to get property of non-object in locale_init() (line 267 of /var/www/d7_dev/modules/locale/locale.module).
# Notice: Trying to get property of non-object in admin_menu_init() (line 143 of /var/www/d7_dev/sites/all/modules/admin_menu/admin_menu.module).
# Notice: Trying to get property of non-object in entity_get_info() (line 6359 of /var/www/d7_dev/includes/common.inc)

I assume the problem seems to be that the $language object should to be determined, by my negotation callback hasn't returned anything yet, so $language isn't set yet.

Comments

damien tournoud’s picture

To load an entity, you have to know the content language, so you cannot possibly load an entity to determine the language.

I doubt there are a lot of ways around this.

amitaibu’s picture

Status: Active » Needs review
StatusFileSize
new1.01 KB

Here's a first stab. It might be a "naive" patch, as I still don't fully gork the bootstrapping process.

The patch adds a fallback language default.

Status: Needs review » Needs work

The last submitted patch, 818418-default-language-1.patch, failed testing.

amitaibu’s picture

@DamZ,

Apart of the failing test, is this approach right?

plach’s picture

Status: Needs work » Needs review

Weird failure. Retesting.

Actually I think this might work, but I'd prefer someone more into the bootstrap process give his/her feedback.

+++ includes/bootstrap.inc
@@ -2281,15 +2281,16 @@ function get_t() {
+  // negotiation cllback needs to do a full bootstrap.

"callback"

Powered by Dreditor.

amitaibu’s picture

StatusFileSize
new1.01 KB

Fix typo. Indeed, it seems the tests are now green.

catch’s picture

Patch looks reasonable at first glance. @Damien - why do you need to know the content language to load an entity? Language negotiation is done during view rather than load with translatable fields.

amitaibu’s picture

Bump, as og_language will need this. @DamZ, can you please have a look?

andypost’s picture

If you loading node by ID so language is attribute so it's possible.
Suppose when you loading a node (entity) fields so _field_language_suggestion() should help

plach’s picture

Node language is a fixed value held in the $node->language field, but the value in $language_content is computed at runtime and cannot help to load a node by its language.

amitaibu’s picture

#6: 818418-default-language-6.patch queued for re-testing.

amitaibu’s picture

hmm, tests are still green.

@Damz,
> I doubt there are a lot of ways around this.

Does the patch overcome the doubts you had?

damien tournoud’s picture

Status: Needs review » Reviewed & tested by the community

Ok. I'm still not very convinced that loading an entity before changing the language is really a good idea, but this patch makes sense. We should not actively prevent people from trying this.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

I'm not sure I totally follow why this is necessary, but the resulting code is a lot easier to read. Sounds like everyone is in agreement that this is either a good idea or it shouldn't be actively blocked.

Committed to HEAD. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.