You are here

public function GeofieldWidget::fileParse in farmOS 2.x

Submit function to parse geometries from uploaded files.

Parameters

array $form: The form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

File

modules/core/map/src/Plugin/Field/FieldWidget/GeofieldWidget.php, line 209

Class

GeofieldWidget
Plugin implementation of the map 'geofield' widget.

Namespace

Drupal\farm_map\Plugin\Field\FieldWidget

Code

public function fileParse(array &$form, FormStateInterface $form_state) {

  // Bail if no populate file field is not configured.
  $populate_file_field = $this
    ->getSetting('populate_file_field');
  if (empty($populate_file_field)) {
    return;
  }

  // Get the form field element.
  $triggering_element = $form_state
    ->getTriggeringElement();
  $element = NestedArray::getValue($form, array_slice($triggering_element['#array_parents'], 0, -1));

  // Load the uploaded files.
  $uploaded_files = $form_state
    ->getValue($populate_file_field);
  if (!empty($uploaded_files)) {

    // Get file IDs.
    $file_ids = array_reduce($uploaded_files, function ($carry, $file) {
      return array_merge($carry, array_values($file['fids']));
    }, []);

    // Load and process each file.

    /** @var \Drupal\file\Entity\File[] $files */
    $files = \Drupal::entityTypeManager()
      ->getStorage('file')
      ->loadMultiple($file_ids);

    // @todo Support geometry field with > 1 cardinality.
    $wkt_strings = [];
    if (!empty($files)) {
      foreach ($files as $file) {

        // Get the geometry type.
        $geophp_type = $this
          ->getGeoPhpType($file);

        // Bail if the file is not a supported format.
        if ($geophp_type === FALSE) {
          $this
            ->messenger()
            ->addWarning($this
            ->t('%filename is not a supported geometry file format. Supported formats: %formats', [
            '%filename' => $file
              ->getFilename(),
            '%formats' => implode(', ', array_keys(static::$geoPhpTypes)),
          ]));
          return;
        }

        // Try to parse geometry using the specified geoPHP type.
        $path = $file
          ->getFileUri();
        if ($geophp_type == 'kml' && $file
          ->getMimeType() === 'application/vnd.google-earth.kmz' && extension_loaded('zip')) {
          $path = 'zip://' . $this->fileSystem
            ->realpath($path) . '#doc.kml';
        }
        $data = file_get_contents($path);
        if ($geom = $this->geoPhpWrapper
          ->load($data, $geophp_type)) {
          $wkt_strings[] = $geom
            ->out('wkt');
        }
      }
    }

    // Merge WKT geometries into a single geometry collection.
    $wkt = '';
    if (!empty($wkt_strings)) {
      if (count($wkt_strings) > 1) {
        $wkt = $this
          ->combineWkt($wkt_strings);
      }
      else {
        $wkt = reset($wkt_strings);
      }
    }

    // Bail if no geometry was parsed.
    if (empty($wkt)) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('No geometry could be parsed from %filename.', [
        '%filename' => $file
          ->getFilename(),
      ]));
      return;
    }

    // Unset the current geometry value from the user input.
    $field_name = $this->fieldDefinition
      ->getName();
    $delta = $element['#delta'];
    $user_input = $form_state
      ->getUserInput();
    unset($user_input[$field_name][$delta]['value']);
    $form_state
      ->setUserInput($user_input);

    // Set the new form value.
    $form_state
      ->setValue([
      $field_name,
      $delta,
      'value',
    ], $wkt);

    // Rebuild the form so the map widget is rebuilt with the new value.
    $form_state
      ->setRebuild(TRUE);
  }
}