You are here

public function IpGeoLocViewsPluginStyle::pluginStyleDiffColorTableRowForm in IP Geolocation Views & Maps 8

Diffs the color table plugin style row.

1 call to IpGeoLocViewsPluginStyle::pluginStyleDiffColorTableRowForm()
IpGeoLocViewsPluginStyle::pluginStyleDifferentiatorColorTableForm in src/Services/IpGeoLocViewsPluginStyle.php
Plugin style color differ.

File

src/Services/IpGeoLocViewsPluginStyle.php, line 495

Class

IpGeoLocViewsPluginStyle
Class IpGeoLocViewsPluginStyle.

Namespace

Drupal\ip_geoloc\Services

Code

public function pluginStyleDiffColorTableRowForm($is_openlayers, $row, $differentiator, $association = NULL) {
  $differentiator_value = empty($association) ? NULL : $association['differentiator_value'];
  $attr_class = $is_openlayers ? [
    'differentiator ol',
  ] : [
    'differentiator',
  ];
  if (strpos($differentiator, 'field_') === 0) {

    // @TODO migrate this field. Get entity type
    // $field = field_info_field($differentiator);
    // Remove node and get the info from related view entity type, take care of taxonomies
    $field = FieldStorageConfig::loadByName('node', $differentiator);

    // $instances = ip_geoloc_get_field_instances($differentiator);
    // $instance = reset($instances);
    // @TODO find the correct function
    $widget = $field
      ->getSettings();
    if (strpos($widget['target_type'], 'taxonomy') !== FALSE) {

      // Covers core and Autocomplete Deluxe.
      $vid = '';
      $bundle = 'page';
      $vocs = [];
      $field_confing = FieldConfig::loadByName('node', $bundle, $differentiator);

      // Only using the first vocabulary
      $vocabulary_name = reset($field_confing
        ->getSettings()['handler_settings']['target_bundles']);
      $vocabularies = Vocabulary::loadMultiple();
      foreach ($vocabularies as $vid => $vocabulary) {
        if ($vid == $vocabulary_name) {
          break;
        }
      }
      $options = [];
      $terms = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term')
        ->loadTree($vid);
      foreach ($terms as $term) {
        $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
      }
      $form[$differentiator] = [
        '#type' => empty($options) ? 'textfield' : 'select',
        '#options' => $options,
        '#default_value' => $differentiator_value,
        '#attributes' => [
          'class' => $attr_class,
        ],
        '#title' => $differentiator,
      ];
    }
    else {

      // No label, unless other modules override this.
      $instance['label'] = '';

      // Don't want asterisk to appear.
      $instance['required'] = FALSE;

      // Make sure the text field is wide enough, especially for the case of a
      // range, which needs to receive two values and a separator.
      if (isset($instance['widget']['settings']['size']) && $instance['widget']['settings']['size'] < 15) {
        $instance['widget']['settings']['size'] = 15;
      }
      $items[0] = empty($differentiator_value) ? [] : is_array($differentiator_value) ? $differentiator_value : [
        $differentiator_value,
      ];
      if (isset($instance['default_value'])) {
        $items[0] += is_array($instance['default_value']) && !empty($instance['default_value']) ? reset($instance['default_value']) : $instance['default_value'];
      }
      $form['#parents'] = [];
      $form_state = [];
      $form = field_default_form($instance['entity_type'], NULL, $field, $instance, LANGUAGE_NONE, $items, $form, $form_state);
      if ($field['module'] == 'number' || $field['module'] == 'text') {
        $form[$differentiator][LANGUAGE_NONE][0][key($field['columns'])]['#element_validate'] = [
          'ip_geoloc_range_widget_validate',
        ];
      }
      $form[$differentiator]['#attributes']['class'][] = $attr_class;
    }
  }
  else {

    // Not a field, maybe an expression.
    $form[$differentiator] = [
      '#type' => 'textfield',
      '#size' => 40,
      '#default_value' => $differentiator_value,
      '#element_validate' => [
        'ip_geoloc_range_widget_validate',
      ],
      '#attributes' => [
        'class' => $attr_class,
      ],
      '#title' => $differentiator,
    ];
  }
  $form['color'] = [
    '#type' => 'select',
    '#default_value' => empty($association) ? NULL : $association['color'],
    '#options' => $is_openlayers ? ip_geoloc_openlayers_marker_layers() : ip_geoloc_marker_colors(),
    '#attributes' => [
      'class' => $is_openlayers ? [
        'marker-color-ol',
      ] : [
        'marker-color',
      ],
    ],
  ];
  $form['special char'] = [
    '#type' => 'textfield',
    '#size' => 8,
    '#default_value' => empty($association) ? NULL : $association['special char'],
  ];
  $form['special char class'] = [
    '#type' => 'textfield',
    '#size' => 25,
    '#default_value' => empty($association) ? NULL : $association['special char class'],
  ];

  // We'll manually set the #parents property of these fields so that their
  // values appear in the $form_state->getValue('color_table') array.
  $form[$differentiator]['#parents'] = [
    'color_table',
    $row,
    $differentiator,
  ];
  $form['color']['#parents'] = [
    'color_table',
    $row,
    'color',
  ];
  $form['special char']['#parents'] = [
    'color_table',
    $row,
    'special char',
  ];
  $form['special char class']['#parents'] = [
    'color_table',
    $row,
    'special char class',
  ];
  return $form;
}