You are here

private function GeofieldMapFieldTrait::setMapMarkerAndInfowindowElement in Geofield Map 8

Same name and namespace in other branches
  1. 8.2 src/GeofieldMapFieldTrait.php \Drupal\geofield_map\GeofieldMapFieldTrait::setMapMarkerAndInfowindowElement()

Set Map Marker and Infowindow Element.

Parameters

array $form: The Form array.

array $settings: The Form Settings.

array $elements: The Form element to alter.

1 call to GeofieldMapFieldTrait::setMapMarkerAndInfowindowElement()
GeofieldMapFieldTrait::generateGmapSettingsForm in src/GeofieldMapFieldTrait.php
Generate the Google Map Settings Form.

File

src/GeofieldMapFieldTrait.php, line 723

Class

GeofieldMapFieldTrait
Class GeofieldMapFieldTrait.

Namespace

Drupal\geofield_map

Code

private function setMapMarkerAndInfowindowElement(array $form, array $settings, array &$elements) {

  // Get the configurations of possible entity fields.
  $fields_configurations = $this->entityFieldManager
    ->getFieldStorageDefinitions('node');
  $elements['map_marker_and_infowindow'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Map Marker and Infowindow'),
  ];
  $elements['map_marker_and_infowindow']['icon_image_path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Icon Image'),
    '#size' => '120',
    '#description' => $this
      ->t('Input the Specific Icon Image path (absolute path, or relative to the Drupal site root prefixed with a trailing hash). If not set, or not found/loadable, the Default Google Marker will be used.'),
    '#default_value' => $settings['map_marker_and_infowindow']['icon_image_path'],
    '#placeholder' => 'modules/custom/geofield_map/images/beachflag.png',
    '#element_validate' => [
      [
        get_class($this),
        'urlValidate',
      ],
    ],
  ];
  $multivalue_fields_states = [];
  $infowindow_fields_options = [];
  foreach ($this->infowindowFieldTypesOptions as $field_type) {
    $infowindow_fields_options = array_merge_recursive($infowindow_fields_options, $this->entityFieldManager
      ->getFieldMapByFieldType($field_type));
  }

  // In case it is a Field Formatter.
  if (isset($this->fieldDefinition)) {
    $desc_options = [
      '0' => $this
        ->t('- Any - No Infowindow'),
      'title' => $this
        ->t('- Title -'),
    ];

    // Get the Cardinality set for the Formatter Field.
    $field_cardinality = $this->fieldDefinition
      ->getFieldStorageDefinition()
      ->getCardinality();
    foreach ($infowindow_fields_options[$form['#entity_type']] as $k => $field) {
      if (!empty(array_intersect($field['bundles'], [
        $form['#bundle'],
      ])) && !in_array($k, [
        'title',
        'revision_log',
      ])) {
        $desc_options[$k] = $k;

        /* @var \\Drupal\Core\Field\BaseFieldDefinition $fields_configurations[$k] */
        if ($field_cardinality !== 1 && (isset($fields_configurations[$k]) && $fields_configurations[$k]
          ->getCardinality() !== 1)) {
          $multivalue_fields_states[] = [
            'value' => $k,
          ];
        }
      }
    }
    $desc_options['#rendered_entity'] = $this
      ->t('- Rendered @entity entity -', [
      '@entity' => $form['#entity_type'],
    ]);
    $info_window_source_options = $desc_options;
  }
  else {
    $info_window_source_options = isset($settings['infowindow_content_options']) ? $settings['infowindow_content_options'] : [];
    foreach ($info_window_source_options as $k => $field) {

      /* @var \\Drupal\Core\Field\BaseFieldDefinition $fields_configurations[$k] */
      if (array_key_exists($k, $fields_configurations) && $fields_configurations[$k]
        ->getCardinality() !== 1) {
        $multivalue_fields_states[] = [
          'value' => $k,
        ];
      }
      if (array_key_exists($k, $fields_configurations) && $fields_configurations[$k]
        ->getCardinality() !== 1) {
        $multivalue_fields_states[] = [
          'value' => $k,
        ];
      }

      // Remove the fields options that are not string/text type fields
      // of the view entity type.
      if (isset($this->entityType) && substr($k, 0, 5) == 'field' && !array_key_exists($k, $infowindow_fields_options[$this->entityType])) {
        unset($info_window_source_options[$k]);
      }
    }
  }
  if (!empty($info_window_source_options)) {
    $elements['map_marker_and_infowindow']['infowindow_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Marker Infowindow Content from'),
      '#description' => $this
        ->t('Choose an existing string/text type field from which populate the Marker Infowindow'),
      '#options' => $info_window_source_options,
      '#default_value' => $settings['map_marker_and_infowindow']['infowindow_field'],
    ];
  }
  $elements['map_marker_and_infowindow']['multivalue_split'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Multivalue Field Split (<u>A Multivalue Field as been selected for the Infowindow Content)</u>'),
    '#description' => $this
      ->t('If checked, each field value will be split into each matching infowindow, following the same progressive order<br>(the first value of the field will be used otherwise, or as fallback in case of no match)'),
    '#default_value' => !empty($settings['map_marker_and_infowindow']['multivalue_split']) ? $settings['map_marker_and_infowindow']['multivalue_split'] : 0,
    '#return_value' => 1,
  ];
  if (isset($this->fieldDefinition)) {
    $elements['map_marker_and_infowindow']['multivalue_split']['#description'] = $this
      ->t('If checked, each field value will be split into each matching infowindow / geofield, following the same progressive order<br>(the first value of the field will be used otherwise, or as fallback in case of no match)');
    $elements['map_marker_and_infowindow']['multivalue_split']['#states'] = [
      'visible' => [
        ':input[name="fields[' . $this->fieldDefinition
          ->getName() . '][settings_edit_form][settings][map_marker_and_infowindow][infowindow_field]"]' => $multivalue_fields_states,
      ],
    ];
  }
  else {
    $elements['map_marker_and_infowindow']['multivalue_split']['#description'] = $this
      ->t('If checked, each field value will be split into each matching infowindow /geofield , following the same progressive order<br>(The Multiple Field settings from the View Display will be used otherwise)');
    $elements['map_marker_and_infowindow']['multivalue_split']['#states'] = [
      'visible' => [
        ':input[name="style_options[map_marker_and_infowindow][infowindow_field]"]' => $multivalue_fields_states,
      ],
    ];
  }
  if (isset($this->fieldDefinition)) {

    // Get the human readable labels for the entity view modes.
    $view_mode_options = [];
    foreach ($this->entityDisplayRepository
      ->getViewModes($form['#entity_type']) as $key => $view_mode) {
      $view_mode_options[$key] = $view_mode['label'];
    }

    // The View Mode drop-down is visible conditional on "#rendered_entity"
    // being selected in the Description drop-down above.
    $elements['map_marker_and_infowindow']['view_mode'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('View mode'),
      '#description' => $this
        ->t('View mode the entity will be displayed in the Infowindow.'),
      '#options' => $view_mode_options,
      '#default_value' => !empty($settings['map_marker_and_infowindow']['view_mode']) ? $settings['map_marker_and_infowindow']['view_mode'] : NULL,
      '#states' => [
        'visible' => [
          ':input[name$="[settings][map_marker_and_infowindow][infowindow_field]"]' => [
            'value' => '#rendered_entity',
          ],
        ],
      ],
    ];
  }
  if (isset($this->fieldDefinition)) {
    $elements['map_marker_and_infowindow']['force_open'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Open Infowindow on Load'),
      '#description' => $this
        ->t('If checked the Infowindow will automatically open on page load.<br><b>Note:</b> in case of multivalue Geofield, the Infowindow will be opened (and the Map centered) on the first item.'),
      '#default_value' => !empty($settings['map_marker_and_infowindow']['force_open']) ? $settings['map_marker_and_infowindow']['force_open'] : 0,
      '#return_value' => 1,
    ];
  }
}