You are here

function feeds_para_mapper_feeds_presave in Feeds Paragraphs 7

Creates a revision of the Paragraphs entity when its values are changed.

Implements hook_feeds_presave().

File

./feeds_para_mapper.module, line 1365
Allows feeds to import content to Paragraphs' fields.

Code

function feeds_para_mapper_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) {

  // Throw new Exception("We should not save");
  // If the entity is old, look for the paragraph item and save changes.
  if ($entity->feeds_item->is_new || !isset($entity->updates)) {
    return FALSE;
  }
  $doUpdate = function (\ParagraphsItemEntity $item) {
    $item->revision = TRUE;
    $item->default_revision = TRUE;
    try {
      $parent = $item
        ->hostEntity();
      $pi = new ParagraphsItemEntity();
      $pi = get_class($pi);
      if (isset($parent) && get_class($parent) === $pi) {
        $item
          ->save();
      }
      else {
        $item
          ->save(TRUE);
      }
      $id = array(
        $item->item_id,
      );
      $paragraph = entity_load('paragraphs_item', $id, array(), TRUE);
      $paragraph = reset($paragraph);
      $rev_id = $paragraph->revision_id;
      return $rev_id;
    } catch (Exception $exception) {
      $message = t('Failed to update the paragraph.');
      drupal_set_message($message, 'error');
      drupal_set_message($exception, 'error');
    }
    return NULL;
  };
  $toUpdate = array();
  foreach ($entity->updates as $update) {
    if (!isset($update['paragraph']->is_new)) {
      $toUpdate[] = $update;
    }
  }
  foreach ($toUpdate as $update) {
    $paragraph = $update['paragraph'];
    $doUpdate($paragraph);
  }
  return TRUE;
}