You are here

public function TaxonomyTermThemerUrl::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 MapThemerInterface::buildMapThemerElement

1 method overrides TaxonomyTermThemerUrl::buildMapThemerElement()
TaxonomyTermThemer::buildMapThemerElement in src/Plugin/GeofieldMapThemer/TaxonomyTermThemer.php
Provides a Map Themer Options Element.

File

src/Plugin/GeofieldMapThemer/TaxonomyTermThemerUrl.php, line 123

Class

TaxonomyTermThemerUrl
Style plugin to render a View output as a Leaflet map.

Namespace

Drupal\geofield_map\Plugin\GeofieldMapThemer

Code

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 MapThemer Entity type.
  $entity_type = $geofieldMapView
    ->getViewEntityType();
  $view_fields = $geofieldMapView
    ->getViewFields();

  // Get the field_storage_definitions.
  $field_storage_definitions = $geofieldMapView
    ->getEntityFieldManager()
    ->getFieldStorageDefinitions($entity_type);
  $taxonomy_ref_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 && $field_storage_definitions[$field_id]
      ->getType() == 'entity_reference' && $field_storage_definitions[$field_id]
      ->getSetting('target_type') == 'taxonomy_term' && $field_storage_definitions[$field_id]
      ->getCardinality() == 1) {
      $taxonomy_ref_fields[$field_id] = [];
    }
  }

  // Get the MapThemer Entity bundles.
  $entity_bundles = $this->entityTypeBundleInfo
    ->getBundleInfo($entity_type);

  // Filter the View Bundles based on the View Filtered Bundles,
  // but only if the MapThemer is working on the View base table entity type.
  $view_bundles = $this
    ->getMapThemerEntityBundles($geofieldMapView, $entity_type, $entity_bundles);
  foreach ($taxonomy_ref_fields as $field_id => $data) {
    $taxonomy_ref_fields[$field_id]['target_bundles'] = [];
    foreach ($view_bundles as $bundle) {
      $target_bundles = $this->config
        ->get('field.field.' . $entity_type . '.' . $bundle . '.' . $field_id)
        ->get('settings.handler_settings.target_bundles');
      $target_bundles = is_array($target_bundles) ? array_keys($target_bundles) : [];
      if (!empty($target_bundles)) {
        $taxonomy_ref_fields[$field_id]['target_bundles'] = array_unique(array_merge($taxonomy_ref_fields[$field_id]['target_bundles'], $target_bundles));
      }
    }
  }
  foreach ($taxonomy_ref_fields as $field_id => $data) {
    $taxonomy_ref_fields[$field_id]['terms'] = [];
    foreach ($data['target_bundles'] as $vid) {
      try {
        $taxonomy_terms = [];

        /* @var \Drupal\taxonomy\TermStorageInterface $taxonomy_term_storage */
        $taxonomy_term_storage = $this->entityManager
          ->getStorage('taxonomy_term');

        /* @var \stdClass $term */
        foreach ($taxonomy_term_storage
          ->loadTree($vid) as $term) {
          $taxonomy_terms[$term->tid] = $term->name . (count($data['target_bundles']) > 1 ? ' (vid: ' . $vid . ')' : '');
        }
        $taxonomy_ref_fields[$field_id]['terms'] += $taxonomy_terms;
      } catch (InvalidPluginDefinitionException $e) {
      } catch (PluginNotFoundException $e) {
      }
    }

    // Reorder the field_id referenceable terms 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]['terms']), array_keys($taxonomy_ref_fields[$field_id]['terms']));
      $taxonomy_ref_fields[$field_id]['terms'] = array_replace(array_flip($default_existing_array_keys), $taxonomy_ref_fields[$field_id]['terms']);
    }
  }

  // Define a default taxonomy_field.
  $keys = array_keys($taxonomy_ref_fields);
  $fallback_taxonomy_field = array_shift($keys);
  $default_taxonomy_field = !empty($default_element['taxonomy_field']) ? $default_element['taxonomy_field'] : $fallback_taxonomy_field;

  // Get the eventual ajax user input of the specific taxonomy field.
  $user_input = $form_state
    ->getUserInput();
  $user_input_taxonomy_field = isset($user_input['style_options']) && isset($user_input['style_options']['map_marker_and_infowindow']['theming']['geofieldmap_taxonomy_term']['values']['taxonomy_field']) ? $user_input['style_options']['map_marker_and_infowindow']['theming']['geofieldmap_taxonomy_term']['values']['taxonomy_field'] : NULL;
  $selected_taxonomy_field = isset($user_input_taxonomy_field) ? $user_input_taxonomy_field : $default_taxonomy_field;
  $element = [
    '#type' => 'fieldset',
    '#prefix' => '<div id="taxonomy-themer-wrapper">',
    '#suffix' => '</div>',
  ];
  if (!count($taxonomy_ref_fields) > 0) {
    $element['taxonomy_field'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('At least a Taxonomy Term reference 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['taxonomy_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Taxonomy Field'),
      '#description' => $this
        ->t('Chose the Taxonomy Term reference field to base the Map Theming upon <br>(only the ones <u>with a cardinality of 1</u> are available for theming).'),
      '#options' => array_combine(array_keys($taxonomy_ref_fields), array_keys($taxonomy_ref_fields)),
      '#default_value' => $selected_taxonomy_field,
      '#ajax' => [
        'callback' => [
          static::class,
          'taxonomyFieldOptionsUpdate',
        ],
        'effect' => 'fade',
      ],
    ];
    $label_alias_upload_help = $this
      ->getLabelAliasHelp();
    $file_select_help = $this->markerIcon
      ->getFileSelectHelp();
    $element['taxonomy_field']['fields'] = [];
    foreach ($taxonomy_ref_fields as $k => $field) {

      // Define the Table Header variables.
      $table_settings = [
        'header' => [
          'label' => $this
            ->t('Taxonomy term'),
          'label_alias' => Markup::create($this
            ->t('Term Alias @description', [
            '@description' => $this->renderer
              ->renderPlain($label_alias_upload_help),
          ])),
          'marker_icon' => Markup::create($this
            ->t('Marker Icon @file_select_help', [
            '@file_select_help' => $this->renderer
              ->renderPlain($file_select_help),
          ])),
          'image_style' => '',
        ],
        'tabledrag_group' => 'terms-order-weight',
        'caption' => [
          'title' => [
            '#type' => 'html_tag',
            '#tag' => 'label',
            '#value' => $this
              ->t('Taxonomy terms from @vocabularies', [
              '@vocabularies' => implode(', ', $field['target_bundles']),
            ]),
            'notes' => [
              '#type' => 'html_tag',
              '#tag' => 'div',
              '#value' => $this
                ->t('The - Default Value - will be used as fallback Value/Marker for unset Terms'),
              '#attributes' => [
                'style' => [
                  'style' => 'font-size:0.8em; color: gray; font-weight: normal',
                ],
              ],
            ],
          ],
        ],
      ];

      // Build the Table Header.
      $element['fields'][$k] = [
        '#type' => 'container',
        'terms' => $this
          ->buildTableHeader($table_settings),
      ];

      // Add a Default Value to be used as possible fallback Value/Marker.
      $field['terms']['__default_value__'] = '- Default Value - ';
      $i = 0;
      foreach ($field['terms'] as $tid => $term) {
        $default_row = isset($default_element['fields']) && isset($default_element['fields'][$k]['terms'][$tid]) ? $default_element['fields'][$k]['terms'][$tid] : NULL;
        $icon_file_uri = isset($default_row) && !empty($default_row['icon_file']) ? $default_row['icon_file'] : NULL;

        // Define the table row parameters.
        $row = [
          'id' => "[geofieldmap_taxonomy_term_url][values][fields][{$k}][terms][{$tid}]",
          'label' => [
            'value' => $term,
            'markup' => $term,
          ],
          'weight' => [
            'value' => isset($default_row) && isset($default_row['weight']) ? $default_row['weight'] : $i,
            'class' => $table_settings['tabledrag_group'],
          ],
          'label_alias' => [
            'value' => isset($default_row) && isset($default_row['label_alias']) ? $default_row['label_alias'] : '',
          ],
          'icon_file_uri' => $icon_file_uri,
          'legend_exclude' => [
            'value' => isset($default_row) && isset($default_row['legend_exclude']) ? $default_row['legend_exclude'] : (count($field['terms']) > 10 ? TRUE : FALSE),
          ],
          'attributes' => [
            'class' => [
              'draggable',
            ],
          ],
        ];

        // Builds the table row for the MapThemer.
        $element['fields'][$k]['terms'][$tid] = $this
          ->buildDefaultMapThemerRow($row);
        $i++;
      }

      // Hide the un-selected Taxonomy Term Field options.
      if ($k != $selected_taxonomy_field) {
        $element['fields'][$k]['#attributes']['class'] = [
          'hidden',
        ];
      }
    }
  }
  return $element;
}