public function ListFieldThemer::buildMapThemerElement in Geofield Map 8.2
Provides a Map Themer Options Element.
Parameters
array $defaults: The default values/settings.
array $form: The form array.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
\Drupal\geofield_map\Plugin\views\style\GeofieldGoogleMapViewStyle $geofieldMapView: The Geofield Map View display object.
Return value
array The Map Themer Options Element
Overrides ListFieldThemerUrl::buildMapThemerElement
File
- src/
Plugin/ GeofieldMapThemer/ ListFieldThemer.php, line 44
Class
- ListFieldThemer
- Style plugin to render a View output as a Leaflet map.
Namespace
Drupal\geofield_map\Plugin\GeofieldMapThemerCode
public function buildMapThemerElement(array $defaults, array &$form, FormStateInterface $form_state, GeofieldGoogleMapViewStyle $geofieldMapView) {
// Get the existing (Default) Element settings.
$default_element = $this
->getDefaultThemerElement($defaults);
// Get the View Filtered entity bundles.
$entity_type = $geofieldMapView
->getViewEntityType();
$view_fields = $geofieldMapView
->getViewFields();
// Get the field_storage_definitions.
$field_storage_definitions = $geofieldMapView
->getEntityFieldManager()
->getFieldStorageDefinitions($entity_type);
// Get the defined List Type Fields.
$list_fields = [];
foreach ($view_fields as $field_id => $field_label) {
/* @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
if (isset($field_storage_definitions[$field_id]) && $field_storage_definitions[$field_id] instanceof FieldStorageConfig && in_array($field_storage_definitions[$field_id]
->getType(), [
'list_string',
'list_integer',
'list_float',
]) && $field_storage_definitions[$field_id]
->getCardinality() == 1) {
$list_fields[$field_id] = [
'options' => $field_storage_definitions[$field_id]
->getSetting('allowed_values'),
];
}
}
foreach ($list_fields as $field_id => $field_label) {
// Reorder the field_id options on existing (Default) Element settings.
if (!empty($default_element)) {
// Eventually filter out the default terms that have been removed, in
// the meanwhile.
$default_existing_array_keys = array_intersect(array_keys($default_element['fields'][$field_id]['options']), array_keys($list_fields[$field_id]['options']));
$list_fields[$field_id]['options'] = array_replace(array_flip($default_existing_array_keys), $list_fields[$field_id]['options']);
}
}
// Define a default list_field.
$keys = array_keys($list_fields);
$fallback_list_field = array_shift($keys);
$default_list_field = !empty($default_element['list_field']) ? $default_element['list_field'] : $fallback_list_field;
// Get the eventual ajax user input of the specific list field.
$user_input = $form_state
->getUserInput();
$user_input_list_field = isset($user_input['style_options']) && isset($user_input['style_options']['map_marker_and_infowindow']['theming']['geofieldmap_list_fields']['values']['list_field']) ? $user_input['style_options']['map_marker_and_infowindow']['theming']['geofieldmap_list_fields']['values']['list_field'] : NULL;
$selected_list_field = isset($user_input_list_field) ? $user_input_list_field : $default_list_field;
$element = [
'#type' => 'fieldset',
'#prefix' => '<div id="list-themer-wrapper">',
'#suffix' => '</div>',
];
if (!count($list_fields) > 0) {
$element['list_field'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('At least a List Type field (<u>with a cardinality of 1</u>) should be added to the View to use this Map Theming option.'),
'#attributes' => [
'class' => [
'geofield-map-warning',
],
],
];
}
else {
$element['list_field'] = [
'#type' => 'select',
'#title' => $this
->t('List Type Field'),
'#description' => $this
->t('Choose the List type field to base the Map Theming upon.'),
'#options' => array_combine(array_keys($list_fields), array_keys($list_fields)),
'#default_value' => $selected_list_field,
'#ajax' => [
'callback' => [
static::class,
'listFieldOptionsUpdate',
],
'effect' => 'fade',
],
];
$label_alias_upload_help = $this
->getLabelAliasHelp();
$file_upload_help = $this->markerIcon
->getFileUploadHelp();
$element['list_field']['fields'] = [];
foreach ($list_fields as $k => $field) {
// Define the Table Header variables.
$table_settings = [
'header' => [
'label' => $this
->t('Option'),
'label_alias' => Markup::create($this
->t('Option Alias @description', [
'@description' => $this->renderer
->renderPlain($label_alias_upload_help),
])),
'marker_icon' => Markup::create($this
->t('Marker Icon @file_upload_help', [
'@file_upload_help' => $this->renderer
->renderPlain($file_upload_help),
])),
'image_style' => Markup::create($this
->t('Icon Image Style')),
],
'tabledrag_group' => 'options-order-weight',
'caption' => [
'title' => [
'#type' => 'html_tag',
'#tag' => 'label',
'#value' => $this
->t('Options from @field field', [
'@field' => $k,
]),
'notes' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('The - Default Value - will be used as fallback Value/Marker for unset Options'),
'#attributes' => [
'style' => [
'style' => 'font-size:0.8em; color: gray; font-weight: normal',
],
],
],
],
],
];
// Build the Table Header.
$element['fields'][$k] = [
'#type' => 'container',
'options' => $this
->buildTableHeader($table_settings),
];
// Add a Default Value to be used as possible fallback Value/Marker.
$field['options']['__default_value__'] = '- Default Value - ';
$i = 0;
foreach ($field['options'] as $id => $value) {
$fid = (int) (!empty($default_element['fields'][$k]['options'][$id]['icon_file']['fids'])) ? $default_element['fields'][$k]['options'][$id]['icon_file']['fids'] : NULL;
// Define the table row parameters.
$row = [
'id' => "[geofieldmap_list_fields][values][fields][{$k}][options][{$id}]",
'label' => [
'value' => $value,
'markup' => $value,
],
'weight' => [
'value' => isset($default_element['fields'][$k]['options'][$id]['weight']) ? $default_element['fields'][$k]['options'][$id]['weight'] : $i,
'class' => $table_settings['tabledrag_group'],
],
'label_alias' => [
'value' => isset($default_element['fields'][$k]['options'][$id]['label_alias']) ? $default_element['fields'][$k]['options'][$id]['label_alias'] : '',
],
'icon_file_id' => $fid,
'image_style' => [
'options' => $this->markerIcon
->getImageStyleOptions(),
'value' => isset($default_element['fields'][$k]['options'][$id]['image_style']) ? $default_element['fields'][$k]['options'][$id]['image_style'] : 'geofield_map_default_icon_style',
],
'legend_exclude' => [
'value' => isset($default_element['fields'][$k]['options'][$id]['legend_exclude']) ? $default_element['fields'][$k]['options'][$id]['legend_exclude'] : (count($field['options']) > 10 ? TRUE : FALSE),
],
'attributes' => [
'class' => [
'draggable',
],
],
];
// Builds the table row for the MapThemer.
$element['fields'][$k]['options'][$id] = $this
->buildDefaultMapThemerRow($row);
$i++;
}
// Hide the un-selected List Field options.
if ($k != $selected_list_field) {
$element['fields'][$k]['#attributes']['class'] = [
'hidden',
];
}
}
}
return $element;
}