Created
October 10, 2016 10:39
-
-
Save pustynnykh/2ca0dca936278eef70b0469082fb395f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements Migration::prepare(). | |
* This func fix empty fc's on migrate update operation. | |
* Used as fix while performs updates, | |
* drush mi AlNodeInstitution --idlist="8013" --force --update. | |
*/ | |
public function prepare($entity, stdClass $row) { | |
// Don't run on initial import only on update. | |
if (isset($entity->nid)) { | |
// Load the original node object. | |
$original = node_load($entity->nid); | |
self::removeEmptyFcItems($entity, $original, array('field_fc_al_image')); | |
} | |
} | |
/** | |
* Clear & set field collection items. | |
* | |
* Clear from empty first fc item on entity, | |
* and resolve existing field collections from original entity object. | |
* | |
* @param mixed $entity | |
* Object from migrate that will be updated later. | |
* @param mixed $original | |
* Original entity object loaded via entity_load(). | |
* @param array $fc | |
* An array of field collection names that should be fixed. | |
*/ | |
protected function removeEmptyFcItems($entity, $original, $fc) { | |
foreach ($fc as $fc_name) { | |
if (!empty($original->{$fc_name}[LANGUAGE_NONE]) | |
&& count($original->{$fc_name}[LANGUAGE_NONE]) > 1 | |
) { | |
$original_wrapper = entity_metadata_wrapper( | |
_al_migrate_entity_type_get_name($original), | |
$original | |
); | |
if (!empty($original_wrapper->{$fc_name}[0])) { | |
foreach ($original_wrapper->{$fc_name} as $idx => $fc_item) { | |
if (!empty($fc_item) | |
&& method_exists($fc_item, 'value') | |
) { | |
if ($fc_item->value() === NULL) { | |
unset($original->{$fc_name}[LANGUAGE_NONE][$idx]); | |
} | |
} | |
} | |
} | |
} | |
// Add original field collection data to our current object. | |
$entity->$fc_name = $original->$fc_name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment