Fatal error originating from Media module, but could belong to one of the other related modules in use:

  • Media 7.x-2.16
  • Conditional Fields 7.x-3.0-alpha2
  • Entity Translation 7.x-1.0-rc1

Error:
Fatal error: Call to a member function getActiveLanguage() on a non-object in [..]/sites/all/modules/contrib/media/includes/media.fields.inc on line 130

The error occurs when trying to edit a Dependency with Conditional Fields based on an image field dependee that uses the Media Browser widget.

To reproduce

I have a content type with 2 image fields, a Primary Image and Additional Images. I want the Additional Images to be hidden until the Primary Image has been uploaded. I would like to use Media Browser for both fields. See Issue 1704090 comment #12 for creating a dependency that works properly with images.

Steps:

  • Entity Translation is enabled and used on the site for other content
  • Create a content type, but keep Entity Translation disabled (Multilingual support = Disabled)
  • Add 2 image fields to the content type, both using the Media Browser widget
  • Add a dependency using Conditional Fields where one image field is only visible when the other has a value

At this point when adding the dependency, you'll be redirected to the edit page and the Fatal error will appear. If you change the widget for the dependee from Media Browser to just Image, you can edit the dependency without issue.

The dependency does, however, work as expected despite the fact that you can't edit it. So, the workaround is to change the widget to Image, edit and save the dependency, then change the widget back to Media Browser and all is good in the world.

It looks like line 129 in includes/media.fields.inc may just need to be encased in an if() statement, like this:

<?php
  // If translation is enabled for the entity, store its form/source langcodes
  // on the elements for further usage in media_element_process().
  if (module_invoke('entity_translation', 'enabled', $element['#entity_type'], $element['#entity'])) {
    if ($translation_handler = entity_translation_get_handler($element['#entity_type'], $element['#entity'])) {
      $element['#media_parent_entity_form_langcode'] = $translation_handler->getActiveLanguage();
      if ($source_langcode = $translation_handler->getSourceLanguage()) {
        $element['#media_parent_entity_source_langcode'] = $source_langcode;
      }
    }
  }
?>
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

.bert created an issue. See original summary.

joseph.olstad’s picture

try upgrading entity_translation to the latest version 1.0 or higher.

also, try upgrading conditional fields module to the latest dev release, check for patches, I suspect the issue is not with media but with the conditional fields module.

If that doesn't work, can you please roll a patch with your suggested changes. Here is how to write a patch, see "Making a Drupal patch with Git"

harshil.maradiya’s picture

Tre this version "7.x-1.0-beta7" it may resolve your issue

joseph.olstad’s picture

@harshil.maradiya, 1.0-beta7 is not recommended.

Please roll your patch for 2.x,3.x,4.x, the instructions on making a patch are here:

"Making a Drupal patch with Git"

.bert’s picture

Thanks Joseph!

Patch #6 looks good and resolves the issue when applied to Media 7.x-2.16.

joseph.olstad’s picture

Status: Needs review » Fixed

  • joseph.olstad committed e1df55f on 7.x-4.x authored by .bert
    Issue #2988670 by .bert: Fatal Error 'getActiveLanguage() on a non-...

  • joseph.olstad committed 38771aa on 7.x-3.x authored by .bert
    Issue #2988670 by .bert: Fatal Error 'getActiveLanguage() on a non-...

  • joseph.olstad committed cb4aa58 on 7.x-2.x authored by .bert
    Issue #2988670 by .bert: Fatal Error 'getActiveLanguage() on a non-...

Status: Fixed » Closed (fixed)

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