You are here

map_widget.install in Map Widget 8

Install, update and uninstall functions for the map_widget module.

File

map_widget.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the map_widget module.
 */
use Drupal\Core\Entity\Entity\EntityFormDisplay;

/**
 * Untangles recursively nested map data.
 */
function map_widget_update_8101() {
  $form_displays = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->loadMultiple(NULL);
  $update_definitions = [];
  foreach ($form_displays as $display) {
    if (!$display instanceof EntityFormDisplay) {
      throw new UnexpectedValueException('Loaded display not an instance of EntityFormDisplay');
    }

    // Map fields are only base fields.

    /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $definitions */
    $definitions = \Drupal::service('entity_field.manager')
      ->getBaseFieldDefinitions($display
      ->getTargetEntityTypeId());
    $components = $display
      ->getComponents();
    foreach ($components as $field_name => $component) {
      if ($component['type'] === 'map_assoc_widget') {
        $update_definitions[] = $definitions[$field_name];
      }
    }
    foreach ($update_definitions as $field_definition) {
      $storage = \Drupal::entityTypeManager()
        ->getStorage($field_definition
        ->getTargetEntityTypeId());
      $field_name = $field_definition
        ->getName();
      $query = $storage
        ->getQuery();
      $affected_ids = $query
        ->exists($field_name)
        ->execute();
      foreach ($affected_ids as $id) {
        $entity = $storage
          ->load($id);
        $munged_maps = $entity
          ->get($field_name)
          ->getValue();
        $fixed_maps = _map_widget_map_fixer($munged_maps);
        $entity
          ->get($field_name)
          ->setValue($fixed_maps);
        $entity
          ->save();
      }
    }
  }
}

/**
 * Helper function to untangle corrupted maps.
 *
 * @param array $munged_maps
 *   The tangled data.
 *
 * @return array
 *   The untangled data.
 */
function _map_widget_map_fixer(array $munged_maps) {
  $fixed_maps = [];
  $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($munged_maps), RecursiveIteratorIterator::SELF_FIRST);
  foreach ($iterator as $key => $value) {

    // Check for possible normal entry.
    if (is_array($value)) {
      if (isset($value['key']) && isset($value['value']) && !isset($fixed_maps[$value['key']])) {
        $fixed_maps[$value['key']] = $value['value'];
      }
    }
    else {

      // Possible normal entry.
      if ($value == 'value' || $key == 'key') {

        // Processed at a higher level of the iterator.
        continue;
      }
      if (!isset($fixed_maps[$key])) {
        $fixed_maps[$key] = $value;
      }
    }
  }

  // If our repair didn't harvest anything, return the original to prevent loss.
  return empty($fixed_maps) ? $munged_maps : $fixed_maps;
}

Functions

Namesort descending Description
map_widget_update_8101 Untangles recursively nested map data.
_map_widget_map_fixer Helper function to untangle corrupted maps.