You are here

public function FreelinkingManager::createFreelinkElement in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 src/FreelinkingManager.php \Drupal\freelinking\FreelinkingManager::createFreelinkElement()

Create the render array for the respective Freelinking plugin.

@todo Set cache contexts from plugin. @todo Multilingual support.

Parameters

string $plugin_id: The plugin ID.

string $target: The target string to parse.

string $indicator: The indicator string.

string $langcode: The language code.

string $plugin_settings_string: The plugin settings serialized as a string.

string $failover_settings_string: The failover settings serialized as a string. Defaults to an empty array.

Return value

array A render element.

Overrides FreelinkingManagerInterface::createFreelinkElement

File

src/FreelinkingManager.php, line 181

Class

FreelinkingManager
Freelinking plugin manager.

Namespace

Drupal\freelinking

Code

public function createFreelinkElement($plugin_id, $target, $indicator, $langcode, $plugin_settings_string, $failover_settings_string) {
  $plugin_settings = unserialize($plugin_settings_string);
  $configuration = [
    'settings' => isset($plugin_settings['settings']) ? $plugin_settings['settings'] : [],
  ];

  /** @var \Drupal\freelinking\Plugin\FreelinkingPluginInterface $plugin */
  $plugin = $this
    ->createInstance($plugin_id, $configuration);
  $config = $plugin
    ->getConfiguration();
  $target_array = $this
    ->parseTarget($target, $langcode);
  $target_array['indicator'] = $indicator;
  $link = $this
    ->buildLink($plugin, $target_array);
  if (is_array($link) && isset($link['error']) && isset($config['settings']['failover'])) {

    /** @var \Drupal\freelinking\Plugin\FreelinkingPluginInterface $failover_plugin */
    $failover_settings = unserialize($failover_settings_string);
    $failover_plugin_id = $config['settings']['failover'];
    $default_configuration = [
      'settings' => isset($failover_settings['settings']) ? $failover_settings['settings'] : [],
    ];
    if (in_array($failover_plugin_id, $this->builtin)) {

      // Changes plugin indicator to the failover plugin ID for the builtin
      // plugin.
      $failover_plugin_id = 'builtin';
      $target_array['indicator'] = $config['settings']['failover'];
    }
    $failover_plugin = $this
      ->createInstance($failover_plugin_id, $default_configuration);
    $target_array['indicator'] = $config['settings']['failover'];
    $link = $this
      ->buildLink($failover_plugin, $target_array);
  }

  // Drupal currently does not allow returning a render element that only does
  // #pre_render as part of a lazy builder context. This wraps the render
  // array within a small theme function.
  return [
    '#theme' => [
      'freelink',
      'freelink__' . $plugin_id,
    ],
    '#link' => $link,
  ];
}