You are here

public function GeofieldGoogleMapFormatter::settingsForm in Geofield Map 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/GeofieldGoogleMapFormatter.php \Drupal\geofield_map\Plugin\Field\FieldFormatter\GeofieldGoogleMapFormatter::settingsForm()

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form elements for the formatter settings.

Overrides FormatterBase::settingsForm

File

src/Plugin/Field/FieldFormatter/GeofieldGoogleMapFormatter.php, line 258

Class

GeofieldGoogleMapFormatter
Plugin implementation of the 'geofield_google_map' formatter.

Namespace

Drupal\geofield_map\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $default_settings = self::defaultSettings();
  $settings = $this
    ->getSettings();
  $elements = [];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $elements['replacement_patterns'] = [
      '#type' => 'details',
      '#title' => 'Replacement patterns',
      '#description' => $this
        ->t('The following replacement tokens are available for the "Icon Image Path" and the "Map Geometries Options" options'),
    ];
    $elements['replacement_patterns']['token_help'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $this->fieldDefinition
          ->getTargetEntityTypeId(),
      ],
    ];
  }
  else {
    $elements['replacement_patterns']['#description'] = $this
      ->t('The @token_link is needed to browse and use @entity_type entity token replacements.', [
      '@token_link' => $this->link
        ->generate($this
        ->t('Token module'), Url::fromUri('https://www.drupal.org/project/token', [
        'absolute' => TRUE,
        'attributes' => [
          'target' => 'blank',
        ],
      ])),
      '@entity_type' => $this->fieldDefinition
        ->getTargetEntityTypeId(),
    ]);
  }
  $elements += $this
    ->generateGMapSettingsForm($form, $form_state, $settings, $default_settings);
  $elements['#attached'] = [
    'library' => [
      'geofield_map/geofield_map_view_display_settings',
    ],
  ];

  // Define a specific default_icon_image_mode that consider icon_image_path
  // eventually set previously to its select introduction.
  $init_icon_image_mode = !empty($settings['map_marker_and_infowindow']['icon_image_path']) ? 'icon_image_path' : $default_settings['map_marker_and_infowindow']['icon_image_mode'];
  $default_icon_image_mode = !empty($settings['map_marker_and_infowindow']['icon_image_mode']) ? $settings['map_marker_and_infowindow']['icon_image_mode'] : $init_icon_image_mode;
  $geofield_id = $this->fieldDefinition
    ->getName();
  $form_state
    ->setTemporaryValue('geofield_id', $geofield_id);

  // Get the eventual ajax user input of the icon_image_mode field.
  $user_input = $form_state
    ->getUserInput();
  $user_input_icon_image_mode = isset($user_input['fields']) && isset($user_input['fields'][$geofield_id]['settings_edit_form']) && isset($user_input['fields'][$geofield_id]['settings_edit_form']['settings']['map_marker_and_infowindow']['icon_image_mode']) ? $user_input['fields'][$geofield_id]['settings_edit_form']['settings']['map_marker_and_infowindow']['icon_image_mode'] : NULL;
  $selected_icon_image_mode = isset($user_input_icon_image_mode) ? $user_input_icon_image_mode : $default_icon_image_mode;
  $elements['map_marker_and_infowindow']['icon_image_mode'] = [
    '#title' => $this
      ->t('Custom Icon definition mode'),
    '#type' => 'select',
    '#options' => [
      'icon_file' => $this
        ->t('Icon File'),
      'icon_image_path' => $this
        ->t('Icon Image Path'),
    ],
    '#default_value' => $selected_icon_image_mode,
    '#description' => [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => Markup::create('choose method between:<br><b>Icon Image Path:</b> Point the image url (absolute or relative to Drupal root folder)<br><b>Icon Image File:</b> Upload an Icon Image into Drupal application</li>'),
    ],
    '#weight' => $elements['map_marker_and_infowindow']['icon_image_path']['#weight'] - 2,
    '#ajax' => [
      'callback' => [
        static::class,
        'iconImageModeUpdate',
      ],
      'effect' => 'fade',
    ],
  ];
  $file_upload_help = $this->markerIcon
    ->getFileUploadHelp();
  $fid = (int) (!empty($settings['map_marker_and_infowindow']['icon_file_wrapper']['icon_file']['fids'])) ? $settings['map_marker_and_infowindow']['icon_file_wrapper']['icon_file']['fids'] : NULL;
  $elements['map_marker_and_infowindow']['icon_file_wrapper'] = [
    '#type' => 'container',
    'label' => [
      '#markup' => Markup::create($this
        ->t('<label>Custom Icon Image File</label>')),
    ],
    'description' => [
      '#markup' => Markup::create($this
        ->t('The chosen icon file will be used as Marker for this content @file_upload_help', [
        '@file_upload_help' => $this->renderer
          ->renderPlain($file_upload_help),
      ])),
    ],
    'icon_file' => $this->markerIcon
      ->getIconFileManagedElement($fid),
    'image_style' => [
      '#type' => 'select',
      '#title' => $this
        ->t('Image style'),
      '#options' => $this->markerIcon
        ->getImageStyleOptions(),
      '#default_value' => isset($settings['map_marker_and_infowindow']['icon_file_wrapper']['image_style']) ? $settings['map_marker_and_infowindow']['icon_file_wrapper']['image_style'] : 'geofield_map_default_icon_style',
      '#states' => [
        'visible' => [
          ':input[name="fields[field_geofield][settings_edit_form][settings][map_marker_and_infowindow][icon_file_wrapper][icon_file][is_svg]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ],
    'image_style_svg' => [
      '#type' => 'container',
      'warning' => [
        '#markup' => $this
          ->t("Image style cannot apply to SVG Files,<br>SVG natural dimension will be applied."),
      ],
      '#states' => [
        'invisible' => [
          ':input[name="fields[field_geofield][settings_edit_form][settings][map_marker_and_infowindow][icon_file_wrapper][icon_file][is_svg]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ],
    '#weight' => $elements['map_marker_and_infowindow']['icon_image_mode']['#weight'] + 1,
  ];
  if ($selected_icon_image_mode != 'icon_file') {
    $elements['map_marker_and_infowindow']['icon_file_wrapper']['#attributes']['class'] = [
      'hidden',
    ];
  }
  if ($selected_icon_image_mode != 'icon_image_path') {
    $elements['map_marker_and_infowindow']['icon_image_path']['#prefix'] = '<div id="icon-image-path" class="visually-hidden">';
    $elements['map_marker_and_infowindow']['icon_image_path']['#suffix'] = '</div>';
  }
  return $elements + parent::settingsForm($form, $form_state);
}