You are here

class GeofieldMapLegend in Geofield Map 8.2

Provides a custom Geofield Map Legend block.

Plugin annotation


@Block(
  id = "geofield_map_legend",
  admin_label = @Translation("Geofield Map Legend"),
  category = @Translation("Geofield Map")
)

Hierarchy

Expanded class hierarchy of GeofieldMapLegend

File

src/Plugin/Block/GeofieldMapLegend.php, line 31

Namespace

Drupal\geofield_map\Plugin\Block
View source
class GeofieldMapLegend extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * The Current User.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The Link generator Service.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $link;

  /**
   * The Renderer service property.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The MapThemer Manager service .
   *
   * @var \Drupal\geofield_map\MapThemerPluginManager
   */
  protected $mapThemerManager;

  /**
   * The MapThemer Plugin used in referenced View Style.
   *
   * @var \Drupal\geofield_map\MapThemerInterface
   */
  protected $mapThemerPlugin;

  /**
   * Get the MapThemer Plugin used in referenced View Style.
   *
   * @param string $view_id
   *   The View Id.
   * @param string $view_display_id
   *   The View Display Id.
   *
   * @return array|null
   *   The MapThemer Plugin, or null.
   */
  protected function getMapThemerPluginAndValues($view_id, $view_display_id) {
    $view_displays = $this->config
      ->get('views.view.' . $view_id)
      ->get('display');
    if (!empty($view_displays) && !empty($view_displays[$view_display_id]) && isset($view_displays[$view_display_id]['display_options']['style'])) {
      $view_options = $view_displays[$view_display_id]['display_options']['style']['options'];
      $plugin_id = isset($view_options['map_marker_and_infowindow']['theming']) ? $view_options['map_marker_and_infowindow']['theming']['plugin_id'] : NULL;
      if (isset($plugin_id) && $plugin_id != 'none' && isset($view_options['map_marker_and_infowindow']['theming'][$plugin_id])) {
        try {

          /* @var \Drupal\geofield_map\MapThemerInterface $mapThemerPlugin */
          $plugin = $this->mapThemerManager
            ->createInstance($plugin_id);
          $theming_values = isset($view_options['map_marker_and_infowindow']['theming'][$plugin_id]['values']) ? $view_options['map_marker_and_infowindow']['theming'][$plugin_id]['values'] : NULL;
          return [
            $plugin,
            $theming_values,
          ];
        } catch (\Exception $e) {
          watchdog_exception('Geofield Map Legend', $e);
          return NULL;
        }
      }
    }
  }

  /**
   * Legend Failure element..
   *
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $failure_message
   *   The View Id.
   *
   * @return array|null
   *   The MapThemer Plugin, or null.
   */
  protected function legendFailureElement(TranslatableMarkup $failure_message = NULL) {
    if (!isset($failure_message)) {
      $failure_message = $this
        ->t("The Legend can't be rendered");
    }
    $legend_failure = $this->currentUser
      ->hasPermission('configure geofield_map') ? [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $failure_message,
      '#attributes' => [
        'class' => [
          'geofield-map-message legend-failure-message',
        ],
      ],
    ] : [];
    return $legend_failure;
  }

  /**
   * The Icon Managed File Service.
   *
   * @var \Drupal\geofield_map\Services\MarkerIconService
   */
  protected $markerIcon;

  /**
   * Creates a LocalActionsBlock instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The Current User.
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
   *   The Link Generator service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The Renderer service.
   * @param \Drupal\geofield_map\MapThemerPluginManager $map_themer_manager
   *   The mapThemerManager service.
   * @param \Drupal\geofield_map\Services\MarkerIconService $marker_icon_service
   *   The Marker Icon Service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, AccountInterface $current_user, LinkGeneratorInterface $link_generator, RendererInterface $renderer, MapThemerPluginManager $map_themer_manager, MarkerIconService $marker_icon_service) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->config = $config_factory;
    $this->currentUser = $current_user;
    $this->link = $link_generator;
    $this->renderer = $renderer;
    $this->mapThemerManager = $map_themer_manager;
    $this->markerIcon = $marker_icon_service;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'), $container
      ->get('current_user'), $container
      ->get('link_generator'), $container
      ->get('renderer'), $container
      ->get('plugin.manager.geofield_map.themer'), $container
      ->get('geofield_map.marker_icon'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'label_display' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {

    // Attach Geofield Map Libraries.
    $form['#attached']['library'][] = 'geofield_map/geofield_map_general';
    $form['#attached']['library'][] = 'geofield_map/geofield_map_legend';
    $user_input = $form_state
      ->getUserInput();
    $user_selected_geofield_map_legend = !empty($user_input['settings']['geofield_map_legend']) ? $user_input['settings']['geofield_map_legend'] : NULL;
    $geofield_map_legends = $this
      ->getGeofieldMapLegends();
    $selected_geofield_map_legend = isset($user_selected_geofield_map_legend) ? $user_selected_geofield_map_legend : (isset($this->configuration['geofield_map_legend']) && array_key_exists($this->configuration['geofield_map_legend'], $geofield_map_legends) ? $this->configuration['geofield_map_legend'] : 'none:none');
    list($view_id, $view_display_id) = explode(':', $selected_geofield_map_legend);
    $map_themer_plugin_and_values = $this
      ->getMapThemerPluginAndValues($view_id, $view_display_id);
    $map_themer_plugin = NULL;
    if (!empty($map_themer_plugin_and_values)) {

      /* @var \Drupal\geofield_map\MapThemerInterface $map_themer_plugin */
      $map_themer_plugin = $map_themer_plugin_and_values[0];
      $map_themer_plugin_definition = $map_themer_plugin
        ->getPluginDefinition();
    }
    $geofield_map_legends_options = array_merge([
      'none:none' => '_ none _',
    ], $geofield_map_legends);
    $form['#prefix'] = '<div id="geofield-map-legend-settings-block-wrapper">';
    $form['#suffix'] = '</div>';
    if (!empty($geofield_map_legends)) {
      $form['geofield_map_legend'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Geofield Map Legend'),
        '#description' => $this
          ->t('Select the Geofield Map legend to render in this block<br>Choose the View and the Display you want to grab the Legend definition from.'),
        '#options' => $geofield_map_legends_options,
        '#default_value' => $selected_geofield_map_legend ?: 'none',
        '#required' => TRUE,
        '#ajax' => [
          'callback' => [
            static::class,
            'mapLegendSelectionUpdate',
          ],
          'effect' => 'fade',
        ],
      ];
      $form['values_label'] = [
        '#title' => $this
          ->t('Values Column Label'),
        '#type' => 'textfield',
        '#description' => $this
          ->t('Set the Label text to be shown for the Values column. Empty for any Label.'),
        '#default_value' => isset($this->configuration['values_label']) ? $this->configuration['values_label'] : $this
          ->t('Value'),
        '#size' => 26,
      ];
      $form['markers_label'] = [
        '#title' => $this
          ->t('Markers Column Label'),
        '#type' => 'textfield',
        '#description' => $this
          ->t('Set the Label text to be shown for the Markers/Icon column. Empty for any Label.'),
        '#default_value' => isset($this->configuration['markers_label']) ? $this->configuration['markers_label'] : $this
          ->t('Marker/Icon'),
        '#size' => 26,
      ];

      // Define the list of possible legend icon image style.
      $markers_image_style_options = array_merge([
        '_map_theming_image_style_' => '<- Reflect the Map Theming Icon Image Styles ->',
      ], $this->markerIcon
        ->getImageStyleOptions());

      // Force add the geofield_map_default_icon_style, if not existing.
      if (!in_array('geofield_map_default_icon_style', array_keys($markers_image_style_options))) {
        $markers_image_style_options['geofield_map_default_icon_style'] = 'geofield_map_default_icon_style';
      }
      if ($map_themer_plugin instanceof MapThemerInterface && $map_themer_plugin_definition['markerIconSelection']['type'] == 'managed_file') {
        $form['markers_image_style'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Markers Image style'),
          '#options' => $markers_image_style_options,
          '#default_value' => isset($this->configuration['markers_image_style']) ? $this->configuration['markers_image_style'] : 'geofield_map_default_icon_style',
          '#description' => $this
            ->t('Choose the image style the markers icons will be rendered in the Legend with.'),
        ];
      }
      if ($map_themer_plugin instanceof MapThemerInterface && $map_themer_plugin_definition['markerIconSelection']['type'] == 'file_uri') {
        $form['markers_width'] = [
          '#type' => 'number',
          '#title' => $this
            ->t('Markers Width (pixels)'),
          '#default_value' => isset($this->configuration['markers_width']) ? $this->configuration['markers_width'] : 50,
          '#description' => $this
            ->t('Choose the max image width for the marker in the legend.<br>(Empty value for natural image weight.)'),
          '#min' => 10,
          '#max' => 300,
          '#size' => 4,
          '#step' => 5,
        ];
      }
      $form['legend_caption'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Legend Caption'),
        '#description' => $this
          ->t('Write here the Table Legend Caption).'),
        '#default_value' => isset($this->configuration['legend_caption']) ? $this->configuration['legend_caption'] : '',
        '#rows' => 1,
      ];
      $form['legend_notes'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Legend Notes'),
        '#description' => $this
          ->t("Write here Notes to the Legend (Footer as default, might be altered in the Map Themer plugin)."),
        '#default_value' => isset($this->configuration['legend_notes']) ? $this->configuration['legend_notes'] : '',
        '#rows' => 3,
      ];
    }
    else {
      $form['geofield_map_legend_warning'] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#value' => $this
          ->t('No eligible Geofield Map View Style and Theming have been defined/found.<br>Please @define_view_link with Geofield Map View Style and (not null) Theming to be able to choose at least a Legend to render.', [
          '@define_view_link' => $this->link
            ->generate($this
            ->t('define one View'), Url::fromRoute('entity.view.collection')),
        ]),
        '#attributes' => [
          'class' => [
            'geofield-map-warning',
          ],
        ],
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $legend = [];
    $build = [
      '#type' => 'container',
      '#attached' => [
        'library' => [
          'geofield_map/geofield_map_general',
        ],
      ],
    ];
    $selected_geofield_map_legend = $this->configuration['geofield_map_legend'];
    if (!empty($selected_geofield_map_legend)) {
      list($view_id, $view_display_id) = explode(':', $selected_geofield_map_legend);
      $failure_message = $this
        ->t("The Legend can't be rendered because the chosen [@view_id:@view_display_id] view & view display combination don't exists or correspond to a valid Geofield Map Legend anymore. <u>Please reconfigure this Geofield Map Legend block consequently.</u>", [
        '@view_id' => $view_id,
        '@view_display_id' => $view_display_id,
      ]);
      $legend_failure = $this
        ->legendFailureElement($failure_message);
      $map_themer_plugin_and_values = $this
        ->getMapThemerPluginAndValues($view_id, $view_display_id);
      if (!empty($map_themer_plugin_and_values)) {

        /* @var \Drupal\geofield_map\MapThemerInterface $map_themer_plugin */
        list($map_themer_plugin, $theming_values) = $map_themer_plugin_and_values;
        $legend = !empty($theming_values) ? $map_themer_plugin
          ->getLegend($theming_values, $this->configuration) : $this
          ->legendFailureElement($this
          ->t('The legend cannot be rendered due to a wrong setup of the Map Themer in the ViewStyle'));
      }
      else {
        $legend = $legend_failure;
      }
    }
    $build['legend'] = $legend;
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['geofield_map_legend'] = $form_state
      ->getValue('geofield_map_legend');
    $this->configuration['legend_caption'] = $form_state
      ->getValue('legend_caption');
    $this->configuration['legend_notes'] = $form_state
      ->getValue('legend_notes');
    $this->configuration['values_label'] = $form_state
      ->getValue('values_label');
    $this->configuration['markers_label'] = $form_state
      ->getValue('markers_label');
    $this->configuration['markers_image_style'] = $form_state
      ->getValue('markers_image_style');
    $this->configuration['markers_width'] = $form_state
      ->getValue('markers_width');
  }

  /**
   * Get elegible Geofield Map legends.
   *
   * Find of Geofield Map Views Styles where a theming has
   * been defined and outputs them in the form of view_id:view_display_id array
   * list.
   *
   * @return array
   *   The legends list.
   */
  protected function getGeofieldMapLegends() {
    $geofield_legends = [];
    $enabled_views = Views::getEnabledViews();

    /* @var \Drupal\views\Entity\View $view */
    foreach ($enabled_views as $view_id => $view) {
      foreach ($this->config
        ->get('views.view.' . $view_id)
        ->get('display') as $id => $view_display) {
        if (isset($view_display['display_options']['style']) && $view_display['display_options']['style']['type'] == 'geofield_google_map') {
          $view_options = $view_display['display_options']['style']['options'];
          $plugin_id = isset($view_options['map_marker_and_infowindow']['theming']) ? $view_options['map_marker_and_infowindow']['theming']['plugin_id'] : NULL;
          if (isset($plugin_id) && $plugin_id != 'none') {
            $geofield_legends[$view_id . ':' . $id] = $view
              ->label() . ' - display: ' . $id;
          }
        }
      }
    }
    return $geofield_legends;
  }

  /**
   * Ajax callback triggered by Geofield Map Legend Selection.
   *
   * @param array $form
   *   The build form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   Ajax response with updated form element.
   */
  public static function mapLegendSelectionUpdate(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $response
      ->addCommand(new ReplaceCommand('#geofield-map-legend-settings-block-wrapper', $form['settings']));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPluginInterface::BLOCK_LABEL_VISIBLE constant Indicates the block label (title) should be displayed to end users.
BlockPluginTrait::$transliteration protected property The transliteration service.
BlockPluginTrait::access public function
BlockPluginTrait::baseConfigurationDefaults protected function Returns generic default configuration for block plugins.
BlockPluginTrait::blockAccess protected function Indicates whether the block should be shown. 16
BlockPluginTrait::blockValidate public function 3
BlockPluginTrait::buildConfigurationForm public function Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. 2
BlockPluginTrait::calculateDependencies public function
BlockPluginTrait::getConfiguration public function 1
BlockPluginTrait::getMachineNameSuggestion public function 1
BlockPluginTrait::getPreviewFallbackString public function 3
BlockPluginTrait::label public function
BlockPluginTrait::setConfiguration public function
BlockPluginTrait::setConfigurationValue public function
BlockPluginTrait::setTransliteration public function Sets the transliteration service.
BlockPluginTrait::submitConfigurationForm public function Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit().
BlockPluginTrait::transliteration protected function Wraps the transliteration service.
BlockPluginTrait::validateConfigurationForm public function Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). 1
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GeofieldMapLegend::$config protected property The config factory service.
GeofieldMapLegend::$currentUser protected property The Current User.
GeofieldMapLegend::$link protected property The Link generator Service.
GeofieldMapLegend::$mapThemerManager protected property The MapThemer Manager service .
GeofieldMapLegend::$mapThemerPlugin protected property The MapThemer Plugin used in referenced View Style.
GeofieldMapLegend::$markerIcon protected property The Icon Managed File Service.
GeofieldMapLegend::$renderer protected property The Renderer service property.
GeofieldMapLegend::blockForm public function Overrides BlockPluginTrait::blockForm
GeofieldMapLegend::blockSubmit public function Overrides BlockPluginTrait::blockSubmit
GeofieldMapLegend::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
GeofieldMapLegend::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
GeofieldMapLegend::defaultConfiguration public function Overrides BlockPluginTrait::defaultConfiguration
GeofieldMapLegend::getGeofieldMapLegends protected function Get elegible Geofield Map legends.
GeofieldMapLegend::getMapThemerPluginAndValues protected function Get the MapThemer Plugin used in referenced View Style.
GeofieldMapLegend::legendFailureElement protected function Legend Failure element..
GeofieldMapLegend::mapLegendSelectionUpdate public static function Ajax callback triggered by Geofield Map Legend Selection.
GeofieldMapLegend::__construct public function Creates a LocalActionsBlock instance. Overrides BlockPluginTrait::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2