As documented in #2283489: Add change record for file/image target is missing when upgrading from 7.x-2.0-alpha8, existing file and image mappings were broken by #1080386: Add mapping for title and alt fields on images. and requires these mappings to be recreated with the new :uri option. This is problematic for an importer running on many sites. Attached is a patch against 7.x-2.x-dev that attempts to fix these mappings in an upgrade hook. Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

vinmassaro’s picture

Status: Active » Needs review
FileSize
1.63 KB
vinmassaro’s picture

Title: Fix missing file/image mappings caused by #1080386 » Fix missing file/image mappings caused by #1080386 by adding :uri to mappings
twistor’s picture

Status: Needs review » Needs work

This needs to batch. It can very easily run into problems with memory/time.

vinmassaro’s picture

Status: Needs work » Needs review
FileSize
2.14 KB

Here's an updated patch that batches. Thanks!

twistor’s picture

Status: Needs review » Needs work
$sandbox['importers'] = feeds_importer_load_all(TRUE);

We don't want to store all of the importers. Just the importer ids.

$importer = array_pop($sandbox['importers']);

Which means we should be loading the importer here.

foreach ($config['mappings'] as $id => $mapping) {

processor->getMappings();

if (!strpos($mapping['target'], ':')) {

strpos() === FALSE

$processor->addConfig(array('mappings' => $config['mappings']));
$importer->save();

We should only save the importer if it was changed.

Also, the comments need to wrap at 80 chars.

Nice work!

vinmassaro’s picture

Status: Needs work » Needs review
FileSize
2.66 KB

Ok, I modified it to include your changes and also added two other things:

  • A condition to make sure importers exist before attempting to update, to avoid a nasty error I ran into
  • Added a count of actual updated importers to the status message at the end
vinmassaro’s picture

@twistor: could you give this another review? Since it relies on the change in #1080386: Add mapping for title and alt fields on images., it should be included with it in the next stable release. Thanks!

  • twistor committed fce13c2 on 7.x-2.x authored by vinmassaro
    Issue #2341407 by vinmassaro, twistor: Fix missing file/image mappings...
twistor’s picture

Status: Needs review » Fixed

Rearranged things a little bit. Thanks!

PascalAnimateur’s picture

I get an error when upgrading from 7.x-2.0-alpha8+56-dev to +65-dev on a site with multiple feeds importers because of the 7211 update.

The error I get is:
Call to a member function getMappings() on a non-object in /var/www/html/crires.ulaval.ca.dev/sites/all/modules/contrib/feeds/feeds.install on line 710

Most importers use CSV files, some have XPath and another on imports from Zotero.

Anybody knows why $mappings = $importer->processor->getMappings(); would fail on some feeds importers?
Update It's a disabled ZoteroFeed that's causing the problem since feeds_importer_load returns false for disabled importers. There should be a check for this, something like:

  // Load a single importer.
  $importer = array_pop($sandbox['importers']);
  $importer = feeds_importer_load($importer);
  if (!$importer) {
    // Disabled importer should not be checked
    $sandbox['progress']++;
  } else {
    $mappings = $importer->processor->getMappings();

    foreach ($mappings as $key => $mapping) {
      // Act on mappings that do not contain a colon.
      if (strpos($mapping['target'], ':') !== FALSE) {
        continue;
      }

      // Get field data. Check if $info is empty to weed out non-field mappings
      // like temporary targets.
      if (!$info = field_info_field($mapping['target'])) {
        continue;
      }

      // Act on file or image fields.
      if (in_array($info['type'], array('file', 'image'))) {
        // Add ':uri' to fix the mapping.
        $mappings[$key]['target'] = $mapping['target'] . ':uri';
      }
    }

    // If importer has changed, add the updated config and save it.
    if ($importer->processor->getMappings() !== $mappings) {
      $importer->processor->addConfig(array('mappings' => $mappings));
      $importer->save();
      $sandbox['updated_count']++;
    }

    $sandbox['progress']++;
  }
  // Set the value for finished. If progress == max then finished will be 1,
  // signifying we are done.
  $sandbox['#finished'] = ($sandbox['progress'] / $sandbox['max']);

  • twistor committed 0f9f84c on 7.x-2.x
    Issue #2341407 by vinmassaro, twistor: Fix missing file/image mappings...
twistor’s picture

We're specifically loading all importers, so I switched to using feeds_importer(). I missed that from before. feeds_importer_load() is really only a menu callback.

Thanks for the bug report!

vinmassaro’s picture

Thanks all!

vinmassaro’s picture

Thanks all!

MegaChriz’s picture

Status: Fixed » Closed (fixed)

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