You are here

public static function MetatagImport::importCsvRow in Metatag Import Export CSV 8

Imports a single row of data from the CSV.

Parameters

array $headers: The header row of the CSV file.

array $data: A data row of the CSV file.

Return value

The entity which has been updated by the CSV row.

Throws

\Exception Throws an exception if there was a problem finding or saving the entity.

3 calls to MetatagImport::importCsvRow()
MetatagImport::importCsvBatchOperation in src/MetatagImport.php
Batch callback for parsing the CSV.
MetatagImportTest::testBadData in tests/src/Kernel/MetatagImportTest.php
Tests handling of badly-formed CSV data.
MetatagImportTest::testEntityUpdate in tests/src/Kernel/MetatagImportTest.php
Test entities are updated from the CSV data.

File

src/MetatagImport.php, line 124

Class

MetatagImport
Class for batch process for metatag import.

Namespace

Drupal\metatag_import_export_csv

Code

public static function importCsvRow($headers, $data) {
  $entity_type_index = array_search('entity_type', $headers);
  $entity_id_index = array_search('entity_id', $headers);
  $language_index = array_search('language', $headers);
  $path_alias_index = array_search('path_alias', $headers);
  $entity = [];
  if ($entity_type_index !== FALSE && $entity_id_index !== FALSE) {
    $entity_type = trim($data[$entity_type_index]);
    $entity_id = trim($data[$entity_id_index]);
  }
  if ($path_alias_index !== FALSE) {
    $path_alias = trim($data[$path_alias_index]);
  }
  if (!(!empty($entity_type) && !empty($entity_id) || !empty($path_alias))) {
    throw new \Exception(t("Either both of entity_id and entity_type or path_alias must be specified."));
  }
  if ($language_index !== FALSE) {
    $langcode = trim($data[$language_index]);
  }

  // Resolve the path alias if that is being used.
  if (!empty($path_alias)) {
    $path = \Drupal::service('path_alias.manager')
      ->getPathByAlias($path_alias, $langcode ?? NULL);
    $route_parameters = Url::fromUri("internal:" . $path)
      ->getRouteParameters();
    $entity_type = key($route_parameters);
    if (!\Drupal::service('entity_type.manager')
      ->hasDefinition($entity_type)) {
      throw new \Exception(t("The path alias '@alias' does not correspond to an entity route.", [
        '@alias' => $path_alias,
      ]));
    }
    $entity_id = $route_parameters[$entity_type];
  }
  if (!\Drupal::service('entity_type.manager')
    ->hasDefinition($entity_type)) {
    throw new \Exception(t("The '@entity-type' entity type does not exist.", [
      '@entity-type' => $entity_type,
    ]));
  }

  // Load the entity.
  $entity = \Drupal::service('entity_type.manager')
    ->getStorage($entity_type)
    ->load($entity_id);
  if (!$entity) {
    throw new \Exception(t("No @entity-type with ID @entity-id was found.", [
      '@entity-type' => $entity_type,
      '@entity-id' => $entity_id,
    ]));
  }

  // Get the translation if the language is specified in the CSV data.
  if (!empty($langcode)) {
    if (!$entity
      ->hasTranslation($langcode)) {
      throw new \Exception(t("The @entity-type with ID @entity-id has no @language translation.", [
        '@entity-type' => $entity_type,
        '@entity-id' => $entity_id,
        '@language' => $langcode,
      ]));
    }
    $entity = $entity
      ->getTranslation($langcode);
  }
  $tags = [];
  foreach ($headers as $keys => $values) {
    if (trim($data[$keys]) == '_blank') {
      $tags[$values] = "";
    }
    else {
      $tags[$values] = $data[$keys];
    }
  }

  // Remove non meta tags fields.
  $metatag_machine_name = $tags['field_machine_name'];
  unset($tags['entity_id'], $tags['entity_title'], $tags['entity_type'], $tags['alias'], $tags['field_machine_name']);
  try {
    $entity
      ->set($metatag_machine_name, serialize($tags));
    $entity
      ->save();
  } catch (\Exception $e) {
    throw new \Exception(t("Error saving entity @entity-type @entity-id: '@message'.", [
      '@entity-type' => $entity_type,
      '@entity-id' => $entity_id,
      '@message' => $e
        ->getMessage(),
    ]));
  }
  return $entity;
}