You are here

feeds_para_mapper.module in Feeds Paragraphs 8

Same filename and directory in other branches
  1. 7 feeds_para_mapper.module

Allows mapping to Paragraphs fields.

File

feeds_para_mapper.module
View source
<?php

/**
* @file
* Allows mapping to Paragraphs fields.
*/
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function feeds_para_mapper_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.feeds_para_mapper':
      $filepath = dirname(__FILE__) . '/README.md';
      if (file_exists($filepath)) {
        $readme = file_get_contents($filepath);
      }
      else {
        $filepath = dirname(__FILE__) . '/README.txt';
        if (file_exists($filepath)) {
          $readme = file_get_contents($filepath);
        }
      }
      if (!isset($readme)) {
        return NULL;
      }
      $moduleHandler = Drupal::moduleHandler();
      if ($moduleHandler
        ->moduleExists('markdown')) {
        $filters = $moduleHandler
          ->invoke('markdown', 'filter_info');
        $info = $filters['filter_markdown'];
        if (function_exists($info['process callback'])) {
          $output = $info['process callback']($readme, NULL);
        }
        else {
          $output = '<pre>' . $readme . '</pre>';
        }
      }
      else {
        $output = '<pre>' . $readme . '</pre>';
      }
      return $output;
  }
}

/**
 * Implements hook_feeds_targets_alter().
 *
 * @param \Drupal\feeds\FieldTargetDefinition[] $targets
 */
function feeds_para_mapper_feeds_targets_alter(&$targets) {

  // Collect Paragraphs fields targets to remove from mappings:
  $toRemove = [];
  foreach ($targets as $name => $target) {
    if ($target
      ->getPluginId() === "paragraphs") {
      $field = $target
        ->getFieldDefinition();
      if (!isset($field->fpm_removed)) {
        $toRemove[] = $target;
      }
    }
  }
  if (empty($toRemove)) {
    return;
  }

  // Search for the feed type that contains the above Paragraphs targets:
  $entityTypeManager = Drupal::service('entity_type.manager');
  $storage = $entityTypeManager
    ->getStorage('feeds_feed_type');
  $feedTypes = $storage
    ->loadMultiple();
  $firstTarget = $toRemove[0];
  $feedType = array_filter($feedTypes, function ($item) use ($firstTarget) {
    $mappings = $item
      ->getMappings();
    foreach ($mappings as $mapping) {
      if ($mapping['target'] === $firstTarget
        ->getFieldDefinition()
        ->getName()) {
        return $item;
      }
    }
    return false;
  });
  $feedType = reset($feedType);
  if (!$feedType) {
    return;
  }

  // Remove the default Paragraphs mapping:
  $mappings = $feedType
    ->getMappings();
  $changed = false;
  foreach ($toRemove as $target) {
    $field = $target
      ->getFieldDefinition();
    foreach ($mappings as $delta => $mapping) {
      if ($mapping['target'] === $field
        ->getName()) {
        $feedType
          ->removeMapping($delta);
        $changed = true;
        $field->fpm_removed = TRUE;
      }
    }
  }
  if ($changed) {
    $messenger = Drupal::messenger();
    $messenger
      ->addWarning(t('Mapping has been updated, please refresh the page.'));
    $feedType
      ->save();
  }
}

/**
 * Implements hook_entity_update().
 */
function feeds_para_mapper_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  if (isset($entity->fpm_targets)) {
    $rev = Drupal::service('feeds_para_mapper.revision_handler');
    $rev
      ->handle($entity);
  }
}

Functions

Namesort descending Description
feeds_para_mapper_entity_update Implements hook_entity_update().
feeds_para_mapper_feeds_targets_alter Implements hook_feeds_targets_alter().
feeds_para_mapper_help Implements hook_help().