You are here

public function DiffGeneratorPluginManager::createInstanceForFieldDefinition in Entity Share 8.3

Creates a plugin instance for a field definition.

Creates the instance based on the selected plugin for the field.

Parameters

string $field_type: The field type.

Return value

\Drupal\entity_share_diff\DiffGenerator\DiffGeneratorInterface|null The plugin instance, NULL if none.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Component\Plugin\Exception\PluginException

File

modules/entity_share_diff/src/DiffGenerator/DiffGeneratorPluginManager.php, line 65

Class

DiffGeneratorPluginManager
Provides a DiffManager plugin manager.

Namespace

Drupal\entity_share_diff\DiffGenerator

Code

public function createInstanceForFieldDefinition(string $field_type) {
  if (!isset($this->pluginDefinitions)) {
    foreach ($this
      ->getDefinitions() as $plugin_definition) {
      if (isset($plugin_definition['field_types'])) {

        // Iterate through all the field types this plugin supports
        // and for every such field type add the id of the plugin.
        if (!isset($plugin_definition['weight'])) {
          $plugin_definition['weight'] = 0;
        }
        foreach ($plugin_definition['field_types'] as $id) {
          $this->pluginDefinitions[$id][$plugin_definition['id']]['weight'] = $plugin_definition['weight'];
        }
        $plugins = $this->pluginDefinitions;
      }
    }
  }
  else {
    $plugins = $this->pluginDefinitions;
  }

  // Build a list of all diff plugins supporting the field type of the field.
  $plugin_options = [];
  if (isset($plugins[$field_type])) {

    // Sort the plugins based on their weight.
    uasort($plugins[$field_type], 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
    foreach (array_keys($plugins[$field_type]) as $id) {
      $definition = $this
        ->getDefinition($id, FALSE);

      // Check if the plugin is applicable.
      if (isset($definition['class']) && in_array($field_type, $definition['field_types'])) {
        $plugin_options[$id] = $this
          ->getDefinitions()[$id]['label'];
      }
    }
    $settings = key($plugin_options);
    return $this
      ->createInstance($settings, []);
  }
  return NULL;
}