You are here

private function GeofieldMapFieldTrait::setMapMarkerAndInfowindowElement in Geofield Map 8.2

Same name and namespace in other branches
  1. 8 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 813

Class

GeofieldMapFieldTrait
Class GeofieldMapFieldTrait.

Namespace

Drupal\geofield_map

Code

private function setMapMarkerAndInfowindowElement(array $form, array $settings, array &$elements) {
  $icon_image_path_description = $this
    ->t('Input the Specific Icon Image path (absolute path, or relative to the Drupal site root if not prefixed with the initial slash).');
  $icon_image_path_description .= '<br>' . $this
    ->t('Can be an absolute or relative URL.');
  $token_replacement_disclaimer = $this
    ->t('<b>Note: </b> Using <strong>Replacement Patterns</strong> it is possible to dynamically define the Marker Icon output, with the composition of Marker Icon paths including entity properties or fields values.');
  $icon_image_path_description .= '<br>' . $token_replacement_disclaimer;
  $twig_link = $this->link
    ->generate('Twig', Url::fromUri('http://twig.sensiolabs.org/documentation', [
    'absolute' => TRUE,
    'attributes' => [
      'target' => 'blank',
    ],
  ]));
  $icon_image_path_description .= '<br>' . $this
    ->t('You may include @twig_link.', [
    '@twig_link' => $twig_link,
  ]);
  $elements['map_marker_and_infowindow'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Map Marker and Infowindow'),
    '#prefix' => '<div id="map-marker-and-infowindow-wrapper">',
    '#suffix' => '</div>',
  ];
  $elements['map_marker_and_infowindow']['icon_image_path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Icon Image Path'),
    '#size' => '120',
    '#description' => $icon_image_path_description,
    '#default_value' => $settings['map_marker_and_infowindow']['icon_image_path'],
    '#placeholder' => 'modules/contrib/geofield_map/images/beachflag.png',
    '#element_validate' => [
      [
        get_class($this),
        'urlValidate',
      ],
    ],
    '#weight' => -10,
  ];
  $multivalue_fields_states = [];
  $entities_fields_options = [];
  foreach ($this->infowindowFieldTypesOptions as $field_type) {
    $entities_fields_options = array_merge_recursive($entities_fields_options, $this->entityFieldManager
      ->getFieldMapByFieldType($field_type));
  }

  // Setup the tokens for views fields.
  // Code is snatched from Drupal\views\Plugin\views\field\FieldPluginBase.
  if (!isset($this->fieldDefinition)) {
    $elements['map_marker_and_infowindow']['icon_image_path']['#description'] .= '<br>' . $this
      ->t('Twig notation allows you to define per-row icons (@see this @icon_image_path_issue).', [
      '@icon_image_path_issue' => $this->link
        ->generate('Geofield Map drupal.org issue', Url::fromUri('https://www.drupal.org/project/geofield_map/issues/3074255', [
        'absolute' => TRUE,
        'attributes' => [
          'target' => 'blank',
        ],
      ])),
    ]);
    $options = [];
    $optgroup_fields = (string) t('Fields');
    if (isset($this->displayHandler)) {
      foreach ($this->displayHandler
        ->getHandlers('field') as $id => $field) {

        /* @var \Drupal\views\Plugin\views\field\EntityField $field */
        $options[$optgroup_fields]["{{ {$id} }}"] = substr(strrchr($field
          ->label(), ":"), 2);
      }
    }
    $replacement_output = [];
    if (!empty($options)) {
      $replacement_output[] = [
        '#markup' => '<p>' . $this
          ->t("The following replacement tokens are available. Fields may be marked as <em>Exclude from display</em> if you prefer.") . '</p>',
      ];
      foreach (array_keys($options) as $type) {
        if (!empty($options[$type])) {
          $items = [];
          foreach ($options[$type] as $key => $value) {
            $items[] = $key;
          }
          $item_list = [
            '#theme' => 'item_list',
            '#items' => $items,
          ];
          $replacement_output[] = $item_list;
        }
      }
    }
    $elements['map_marker_and_infowindow']['help'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Replacement patterns'),
      '#value' => $replacement_output,
    ];
  }

  // Add SVG UI file support.
  $elements['map_marker_and_infowindow']['icon_image_path']['#description'] .= !$this->moduleHandler
    ->moduleExists('svg_image') ? '<br>' . $this
    ->t('SVG Files support is disabled. Enabled it with @svg_image_link', [
    '@svg_image_link' => $this->link
      ->generate('SVG Image Module', Url::fromUri('https://www.drupal.org/project/svg_image', [
      'absolute' => TRUE,
      'attributes' => [
        'target' => 'blank',
      ],
    ])),
  ]) : '<br>' . $this
    ->t('SVG Files support enabled.');

  // In case it is a Field Formatter.
  if (isset($this->fieldDefinition)) {

    /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->fieldDefinition
      ->getTargetEntityTypeId();

    // Get the configurations of possible entity fields.
    $fields_configurations = $this->entityFieldManager
      ->getFieldStorageDefinitions($entity);
    $title_options = [
      '0' => $this
        ->t('- Any -'),
      'title' => $this
        ->t('- Title -'),
    ];
    $this_entity_fields_options = $title_options;

    // Get the Cardinality set for the Formatter Field.
    $field_cardinality = $this->fieldDefinition
      ->getFieldStorageDefinition()
      ->getCardinality();
    foreach ($entities_fields_options[$this->fieldDefinition
      ->getTargetEntityTypeId()] as $k => $field) {
      if (!empty(array_intersect($field['bundles'], [
        $form['#bundle'],
      ])) && !in_array($k, [
        'title',
        'revision_log',
      ])) {
        $this_entity_fields_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,
          ];
        }
      }
    }
    $info_window_source_options = $this_entity_fields_options;

    // Add the #rendered_entity option.
    $info_window_source_options['#rendered_entity'] = $this
      ->t('- Rendered @entity entity -', [
      '@entity' => $this->fieldDefinition
        ->getTargetEntityTypeId(),
    ]);
    $info_window_source_description = $this
      ->t('Choose an existing string/text type field from which populate the Marker Infowindow.');
  }
  else {
    $fields_configurations = $this->entityFieldManager
      ->getFieldStorageDefinitions($this->entityType);
    $info_window_source_options = isset($settings['infowindow_content_options']) ? $settings['infowindow_content_options'] : [];
    $info_window_source_description = $this
      ->t('Choose an existing field from which populate the Marker Infowindow.');
    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,
        ];
      }
    }
  }
  $elements['map_marker_and_infowindow']['icon_image_path']['#description'] .= '<br>' . $this
    ->t('If not set, or not found/loadable, the Default Google Marker will be used..');
  if (!empty($info_window_source_options)) {
    $elements['map_marker_and_infowindow']['infowindow_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Marker Infowindow Content from'),
      '#description' => $info_window_source_description,
      '#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 value (as simple text), following the same progressive order. Note: No rewrite, links or replacements patterns might be applied.<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,
      ],
    ];
  }

  // Assure the view_mode to eventually fallback into the (initially defined)
  // $settings['view_mode'].
  $default_view_mode = !empty($settings['view_mode']) ? $settings['view_mode'] : (!empty($settings['map_marker_and_infowindow']['view_mode']) ? $settings['map_marker_and_infowindow']['view_mode'] : NULL);
  if (isset($this->fieldDefinition)) {

    // Get the human readable labels for the entity view modes.
    $view_mode_options = [];
    foreach ($this->entityDisplayRepository
      ->getViewModes($this->fieldDefinition
      ->getTargetEntityTypeId()) 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' => $default_view_mode,
      '#states' => [
        'visible' => [
          ':input[name$="[settings][map_marker_and_infowindow][infowindow_field]"]' => [
            'value' => '#rendered_entity',
          ],
        ],
      ],
    ];
  }
  elseif ($this->entityType) {

    // Get the human readable labels for the entity view modes.
    $view_mode_options = [];
    foreach ($this->entityDisplay
      ->getViewModes($this->entityType) 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'] = [
      '#fieldset' => 'map_marker_and_infowindow',
      '#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' => $default_view_mode,
      '#states' => [
        'visible' => [
          ':input[name="style_options[map_marker_and_infowindow][infowindow_field]"]' => [
            [
              'value' => '#rendered_entity',
            ],
            [
              'value' => '#rendered_entity_ajax',
            ],
          ],
        ],
      ],
    ];
  }
  if (isset($this->fieldDefinition)) {
    $elements['map_marker_and_infowindow']['tooltip_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Marker Tooltip'),
      '#description' => $this
        ->t('Choose the option whose value will appear as Tooltip on hover the Marker.'),
      '#options' => $title_options,
      '#default_value' => $settings['map_marker_and_infowindow']['tooltip_field'],
    ];
    $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,
    ];
  }
  else {
    $elements['map_marker_and_infowindow']['tooltip_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Marker Tooltip'),
      '#description' => $this
        ->t('Choose the option whose value will appear as Tooltip on hover the Marker.'),
      '#options' => array_merge([
        '' => '- Any - No Tooltip',
      ], $this->viewFields),
      '#default_value' => $settings['map_marker_and_infowindow']['tooltip_field'],
    ];
  }
}