You are here

protected function LeafletSettingsElementsTrait::generateIconFormElement in Leaflet 2.1.x

Same name and namespace in other branches
  1. 8 src/LeafletSettingsElementsTrait.php \Drupal\leaflet\LeafletSettingsElementsTrait::generateIconFormElement()
  2. 2.0.x src/LeafletSettingsElementsTrait.php \Drupal\leaflet\LeafletSettingsElementsTrait::generateIconFormElement()

Generate the Leaflet Icon Form Element.

Parameters

array $icon_options: The icon array definition.

Return value

array The Leaflet Icon Form Element.

2 calls to LeafletSettingsElementsTrait::generateIconFormElement()
LeafletDefaultFormatter::settingsForm in src/Plugin/Field/FieldFormatter/LeafletDefaultFormatter.php
Returns a form to configure settings for the formatter.
LeafletMap::buildOptionsForm in modules/leaflet_views/src/Plugin/views/style/LeafletMap.php
Provide a form to edit options for this plugin.

File

src/LeafletSettingsElementsTrait.php, line 354

Class

LeafletSettingsElementsTrait
Class LeafletSettingsElementsTrait.

Namespace

Drupal\leaflet

Code

protected function generateIconFormElement(array $icon_options) {
  $default_settings = $this::getDefaultSettings();
  $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_url_description = $this
    ->t('Can be an absolute or relative URL. <b>If left empty the default Leaflet Marker will be used.</b><br>@token_replacement_disclaimer', [
    '@token_replacement_disclaimer' => $token_replacement_disclaimer,
  ]);
  if (isset($this->fieldDefinition)) {
    $icon_type = ':input[name="fields[' . $this->fieldDefinition
      ->getName() . '][settings_edit_form][settings][icon][iconType]"]';
  }
  else {
    $icon_type = ':input[name="style_options[icon][iconType]"]';
  }
  $element = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Map Icon'),
    'description' => [
      '#markup' => $this
        ->t('For details on the following setup refer to @leaflet_icon_documentation_link', [
        '@leaflet_icon_documentation_link' => $this->leafletService
          ->leafletIconDocumentationLink(),
      ]),
    ],
  ];
  $element['iconType'] = [
    '#type' => 'radios',
    '#title' => t('Icon Source'),
    '#default_value' => isset($icon_options['iconType']) ? $icon_options['iconType'] : $default_settings['icon']['iconType'],
    '#options' => [
      'marker' => 'Icon Image Url/Path',
      'html' => 'Field (html DivIcon)',
      'circle_marker' => $this
        ->t('Circle Marker (@more_info)', [
        '@more_info' => $this->link
          ->generate('more info', Url::fromUri('https://leafletjs.com/reference-1.6.0.html#circlemarker', [
          'absolute' => TRUE,
          'attributes' => [
            'target' => 'blank',
          ],
        ])),
      ]),
    ],
  ];
  $element['iconUrl'] = [
    '#title' => $this
      ->t('Icon URL'),
    '#description' => $icon_url_description,
    '#type' => 'textarea',
    '#rows' => 3,
    '#default_value' => isset($icon_options['iconUrl']) ? $icon_options['iconUrl'] : $default_settings['icon']['iconUrl'],
    '#states' => [
      'visible' => [
        $icon_type => [
          'value' => 'marker',
        ],
      ],
    ],
  ];
  $element['shadowUrl'] = [
    '#title' => $this
      ->t('Icon Shadow URL'),
    '#description' => $icon_url_description,
    '#type' => 'textarea',
    '#rows' => 3,
    '#default_value' => isset($icon_options['shadowUrl']) ? $icon_options['shadowUrl'] : $default_settings['icon']['shadowUrl'],
    '#states' => [
      'visible' => [
        $icon_type => [
          'value' => 'marker',
        ],
      ],
    ],
  ];
  $element['className'] = [
    '#title' => $this
      ->t('Icon Class Name'),
    '#description' => $this
      ->t('A custom class name to assign to both icon and shadow images.<br>Supports <b>Replacement Patterns</b>'),
    '#type' => 'textfield',
    '#default_value' => isset($icon_options['className']) ? $icon_options['className'] : $default_settings['icon']['className'],
    '#states' => [
      'visible' => [
        $icon_type => [
          'value' => 'marker',
        ],
      ],
    ],
  ];
  $element['html'] = [
    '#title' => $this
      ->t('Html'),
    '#type' => 'textarea',
    '#description' => $this
      ->t('Insert here the Html code that will be used as marker html markup. <b>If left empty the default Leaflet Marker will be used.</b><br>@token_replacement_disclaimer', [
      '@token_replacement_disclaimer' => $token_replacement_disclaimer,
    ]),
    '#default_value' => isset($icon_options['html']) ? $icon_options['html'] : $default_settings['icon']['html'],
    '#rows' => 3,
    '#states' => [
      'visible' => [
        $icon_type => [
          'value' => 'html',
        ],
      ],
      'required' => [
        $icon_type => [
          'value' => 'html',
        ],
      ],
    ],
  ];
  $element['html_class'] = [
    '#type' => 'textfield',
    '#title' => t('Marker HTML class'),
    '#description' => t('Required class name for the div used to wrap field output. For multiple classes, separate with a space.'),
    '#default_value' => isset($icon_options['html_class']) ? $icon_options['html_class'] : $default_settings['icon']['html_class'],
    '#states' => [
      'visible' => [
        $icon_type => [
          'value' => 'html',
        ],
      ],
    ],
  ];
  $element['circle_marker_options'] = [
    '#type' => 'textarea',
    '#rows' => 2,
    '#title' => $this
      ->t('Circle Marker Options'),
    '#description' => $this
      ->t('An object literal of Circle Marker options, that comply with the @leaflet_circle_marker_object.<br>The syntax should respect the javascript object notation (json) format.<br>As suggested in the field placeholder, always use double quotes (") both for the indexes and the string values.<br><b>Note: </b> Use <strong>Replacement Patterns</strong> to input dynamic values.<br>Empty value will fallback to default Leaflet Circle Marker style.', [
      '@leaflet_circle_marker_object' => $this->link
        ->generate('Leaflet Circle Marker object', Url::fromUri('https://leafletjs.com/reference-1.6.0.html#circlemarker', [
        'absolute' => TRUE,
        'attributes' => [
          'target' => 'blank',
        ],
      ])),
    ]),
    '#default_value' => isset($icon_options['circle_marker_options']) ? $icon_options['circle_marker_options'] : $default_settings['icon']['circle_marker_options'],
    '#placeholder' => $default_settings['icon']['circle_marker_options'],
    '#element_validate' => [
      [
        get_class($this),
        'jsonValidate',
      ],
    ],
    '#states' => [
      'visible' => [
        $icon_type => [
          'value' => 'circle_marker',
        ],
      ],
    ],
  ];
  if (method_exists($this, 'getProvider') && $this
    ->getProvider() == 'leaflet_views') {
    $twig_link = $this->link
      ->generate('Twig', Url::fromUri('http://twig.sensiolabs.org/documentation', [
      'absolute' => TRUE,
      'attributes' => [
        'target' => 'blank',
      ],
    ]));
    $icon_url_description .= '<br>' . $this
      ->t('You may include @twig_link. You may enter data from this view as per the "Replacement patterns" below.', [
      '@twig_link' => $twig_link,
    ]);
    $element['iconUrl']['#description'] = $icon_url_description;
    $element['shadowUrl']['#description'] = $icon_url_description;

    // Setup the tokens for views fields.
    // Code is snatched from Drupal\views\Plugin\views\field\FieldPluginBase.
    $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);
      }
    }

    // Default text.
    $output = [];

    // We have some options, so make a list.
    if (!empty($options)) {
      $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,
          ];
          $output[] = $item_list;
        }
      }
    }
    $element['help'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Replacement patterns'),
      '#value' => $output,
    ];
  }
  $element['iconSize'] = [
    '#title' => $this
      ->t('Icon Size'),
    '#type' => 'fieldset',
    '#description' => $this
      ->t("Size of the icon image in pixels (if empty the natural icon image size will be used).<br>Both support <b>Replacement Patterns</b> and should end up into an Integer (positive value)"),
  ];
  $element['iconSize']['x'] = [
    '#title' => $this
      ->t('Width'),
    '#type' => 'textfield',
    '#size' => 30,
    '#default_value' => isset($icon_options['iconSize']['x']) ? $icon_options['iconSize']['x'] : NULL,
  ];
  $element['iconSize']['y'] = [
    '#title' => $this
      ->t('Height'),
    '#type' => 'textfield',
    '#size' => 30,
    '#default_value' => isset($icon_options['iconSize']['y']) ? $icon_options['iconSize']['y'] : NULL,
  ];
  $element['iconAnchor'] = [
    '#title' => $this
      ->t('Icon Anchor'),
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#description' => $this
      ->t("The coordinates of the 'tip' of the icon (relative to its top left corner). The icon will be aligned so that this point is at the marker\\'s geographical location.<br>Note: Both the values shouldn't be null to be valid."),
  ];
  $element['iconAnchor']['x'] = [
    '#title' => $this
      ->t('X'),
    '#type' => 'number',
    '#min' => -1000,
    '#max' => 1000,
    '#default_value' => isset($icon_options['iconAnchor']) ? $icon_options['iconAnchor']['x'] : NULL,
  ];
  $element['iconAnchor']['y'] = [
    '#title' => $this
      ->t('Y'),
    '#type' => 'number',
    '#min' => -1000,
    '#max' => 1000,
    '#default_value' => isset($icon_options['iconAnchor']) ? $icon_options['iconAnchor']['y'] : NULL,
  ];
  $element['shadowSize'] = [
    '#title' => $this
      ->t('Shadow Size'),
    '#type' => 'fieldset',
    '#description' => $this
      ->t("Size of the shadow image in pixels (if empty the natural shadow image size will be used). <br>Both support <b>Replacement Patterns</b> and should end up into an Integer (positive value)"),
  ];
  $element['shadowSize']['x'] = [
    '#title' => $this
      ->t('Width'),
    '#type' => 'textfield',
    '#size' => 30,
    '#default_value' => isset($icon_options['shadowSize']['x']) ? $icon_options['shadowSize']['x'] : NULL,
  ];
  $element['shadowSize']['y'] = [
    '#title' => $this
      ->t('Height'),
    '#type' => 'textfield',
    '#size' => 30,
    '#default_value' => isset($icon_options['shadowSize']['y']) ? $icon_options['shadowSize']['y'] : NULL,
  ];
  $element['shadowAnchor'] = [
    '#title' => $this
      ->t('Shadow Anchor'),
    '#type' => 'fieldset',
    '#description' => $this
      ->t("The coordinates of the 'tip' of the shadow (relative to its top left corner) (the same as iconAnchor if not specified).<br>Note: Both the values shouldn't be null to be valid."),
  ];
  $element['shadowAnchor']['x'] = [
    '#title' => $this
      ->t('X'),
    '#type' => 'number',
    '#min' => -1000,
    '#max' => 1000,
    '#default_value' => isset($icon_options['shadowAnchor']) ? $icon_options['shadowAnchor']['x'] : NULL,
  ];
  $element['shadowAnchor']['y'] = [
    '#title' => $this
      ->t('Y'),
    '#type' => 'number',
    '#min' => -1000,
    '#max' => 1000,
    '#default_value' => isset($icon_options['shadowAnchor']) ? $icon_options['shadowAnchor']['y'] : NULL,
  ];
  $element['popupAnchor'] = [
    '#title' => $this
      ->t('Popup Anchor'),
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#description' => $this
      ->t("The coordinates of the point from which popups will 'open', relative to the icon anchor.<br>Note: Both the values shouldn't be null to be valid."),
  ];
  $element['popupAnchor']['x'] = [
    '#title' => $this
      ->t('X'),
    '#type' => 'number',
    '#min' => -1000,
    '#max' => 1000,
    '#default_value' => isset($icon_options['popupAnchor']) ? $icon_options['popupAnchor']['x'] : NULL,
  ];
  $element['popupAnchor']['y'] = [
    '#title' => $this
      ->t('Y'),
    '#type' => 'number',
    '#min' => -1000,
    '#max' => 1000,
    '#default_value' => isset($icon_options['popupAnchor']) ? $icon_options['popupAnchor']['y'] : NULL,
  ];
  return $element;
}