You are here

public function ProviderPluginManager::providersPluginsTableList in Geocoder 8.2

Same name and namespace in other branches
  1. 8.3 src/ProviderPluginManager.php \Drupal\geocoder\ProviderPluginManager::providersPluginsTableList()

Generates the Draggable Table of Selectable Geocoder Plugins.

Parameters

array $enabled_plugins: The list of the enabled plugins machine names.

Return value

array The plugins table list.

File

src/ProviderPluginManager.php, line 113

Class

ProviderPluginManager
Provides a plugin manager for geocoder providers.

Namespace

Drupal\geocoder

Code

public function providersPluginsTableList(array $enabled_plugins) {
  $geocoder_settings_link = $this->link
    ->generate(t('Edit options in the Geocoder configuration page</span>'), Url::fromRoute('geocoder.settings', [], [
    'query' => [
      'destination' => Url::fromRoute('<current>')
        ->toString(),
    ],
  ]));
  $options_field_description = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => $this
      ->t('Object literals in YAML format. @geocoder_settings_link', [
      '@geocoder_settings_link' => $geocoder_settings_link,
    ]),
    '#attributes' => [
      'class' => [
        'options-field-description',
      ],
    ],
  ];
  $caption = [
    'title' => [
      '#type' => 'html_tag',
      '#tag' => 'label',
      '#value' => $this
        ->t('Geocoder plugin(s)'),
    ],
    'caption' => [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('Select the Geocoder plugins to use, you can reorder them. The first one to return a valid value will be used.'),
    ],
  ];
  $element['plugins'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Name'),
      $this
        ->t('Weight'),
      $this
        ->t('Options<br>@options_field_description', [
        '@options_field_description' => $this->renderer
          ->renderRoot($options_field_description),
      ]),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'plugins-order-weight',
      ],
    ],
    '#caption' => $this->renderer
      ->renderRoot($caption),
    // We need this class for #states to hide the entire table.
    '#attributes' => [
      'class' => [
        'js-form-item',
        'geocode-plugins-list',
      ],
    ],
  ];

  // Reorder the plugins promoting the default ones in the proper order.
  $plugins = array_combine($enabled_plugins, $enabled_plugins);
  foreach ($this
    ->getPlugins() as $plugin) {

    // Non-default values are appended at the end.
    $plugins[$plugin['id']] = $plugin;
  }
  $plugins = array_map(function ($plugin, $weight) use ($enabled_plugins) {
    $checked = in_array($plugin['id'], $enabled_plugins);
    return array_merge($plugin, [
      'checked' => $checked,
      'weight' => $checked ? $weight : 0,
      'arguments' => empty($plugin['arguments']) ? (string) $this
        ->t("This plugin doesn't accept arguments.") : Yaml::encode($plugin['arguments']),
    ]);
  }, $plugins, range(0, count($plugins) - 1));
  uasort($plugins, function ($pluginA, $pluginB) {
    $order = strcmp($pluginB['checked'], $pluginA['checked']);
    if (0 === $order) {
      $order = $pluginA['weight'] - $pluginB['weight'];
      if (0 === $order) {
        $order = strcmp($pluginA['name'], $pluginB['name']);
      }
    }
    return $order;
  });
  foreach ($plugins as $plugin) {
    $element['plugins'][$plugin['id']] = [
      'checked' => [
        '#type' => 'checkbox',
        '#title' => $plugin['name'],
        '#default_value' => $plugin['checked'],
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for @title', [
          '@title' => $plugin['name'],
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $plugin['weight'],
        '#delta' => 20,
        '#attributes' => [
          'class' => [
            'plugins-order-weight',
          ],
        ],
      ],
      'arguments' => [
        '#type' => 'html_tag',
        '#tag' => 'pre',
        '#value' => $plugin['arguments'],
      ],
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
    ];
  }
  return $element['plugins'];
}