You are here

public function GeofieldWidget::postBuild in Openlayers 7.3

Invoked after an objects render array is built.

Mostly invoked by the map object.

Parameters

array $build: The array with the build information.

\Drupal\openlayers\Types\ObjectInterface $context: The context of the build. Mostly the map object.

Overrides Base::postBuild

File

modules/openlayers_geofield/src/Plugin/Component/GeofieldWidget/GeofieldWidget.php, line 198
Component: GeofieldWidget.

Class

GeofieldWidget
Class GeofieldWidget.

Namespace

Drupal\openlayers_geofield\Plugin\Component\GeofieldWidget

Code

public function postBuild(array &$build, ObjectInterface $context = NULL) {
  $component = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'openlayers-geofield-' . $context
        ->getId(),
    ),
  );
  $data_type = $this
    ->getOption('dataType', array(
    'WKT' => 'WKT',
  ));
  if (count($data_type) > 1) {
    $component['dataType'] = array(
      '#type' => 'select',
      '#title' => 'Data type',
      '#options' => array_intersect_key(array(
        'WKT' => 'WKT',
        'GeoJSON' => 'GeoJSON',
        'KML' => 'KML',
        'GPX' => 'GPX',
      ), $data_type),
      '#attributes' => array(
        'class' => array(
          'data-type',
        ),
      ),
      '#default_value' => $data_type,
    );
  }
  else {
    $parents = $this
      ->getOption('parents');
    $parents[] = 'dataType';
    $component['dataType'] = array(
      '#parents' => $parents,
      '#type' => 'hidden',
      '#default_value' => reset($data_type),
      '#value' => reset($data_type),
      '#attributes' => array(
        'class' => array(
          'data-type',
        ),
      ),
    );
  }
  $geom = $this
    ->initialDataToGeomFeatures();
  $wkt = '';
  if (!empty($geom['geom']) && !$geom['geom']
    ->isEmpty()) {
    $wkt = $geom['geom']
      ->out('wkt');
  }
  $parents = $this
    ->getOption('parents');
  $parents[] = 'geom';
  $component['data'] = array(
    '#parents' => $parents,
    '#type' => $this
      ->getOption('showInputField') ? 'textarea' : 'hidden',
    '#title' => 'Data',
    '#attributes' => array(
      'class' => array(
        'openlayers-geofield-data',
      ),
    ),
    '#default_value' => $wkt,
  );

  // Now add the component into the build array. This is a bit complex due
  // the fact that we want to support form nesting.
  $parents = array(
    'geofield',
    'component',
  );
  $data_input_field_name = $this
    ->getOption('inputFieldName');
  if (!empty($data_input_field_name)) {
    $data_input_field_name = preg_replace('/(^\\[|\\]$)/', '', $data_input_field_name);
    $levels = explode('][', $data_input_field_name);
    $parents = array_slice(explode('][', $data_input_field_name), 0, count($levels) - 1);

    // Ensure the requested name for the input data field is set.
    $component[end($levels)] = $component['data'];
    unset($component['data']);
  }
  if (!empty($parents)) {
    drupal_array_set_nested_value($build, $parents, $component);
  }
  else {
    $build['parameters'][$this
      ->getPluginId()] = $component;
  }
}