function CKEditor5PluginManager::findPluginSupportingElement

Same name and namespace in other branches
  1. 10 core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::findPluginSupportingElement()
  2. 11.x core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::findPluginSupportingElement()

Overrides CKEditor5PluginManagerInterface::findPluginSupportingElement

File

core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php, line 246

Class

CKEditor5PluginManager
Provides a CKEditor 5 plugin manager.

Namespace

Drupal\ckeditor5\Plugin

Code

public function findPluginSupportingElement(string $tag) : ?string {
    // This will contain the element config for a plugin found to support $tag,
    // so it can be compared to additional plugins that support $tag so the
    // plugin with the most permissive config can be the id returned.
    $selected_provided_elements = [];
    $plugin_id = NULL;
    foreach ($this->getDefinitions() as $id => $definition) {
        $provided_elements = $this->getProvidedElements([
            $id,
        ]);
        // Multiple plugins may support the $tag being searched for.
        if (array_key_exists($tag, $provided_elements)) {
            // Skip plugins with conditions as those plugins can't be guaranteed to
            // provide a given tag without additional criteria being met. In the
            // future we could possibly add support for automatically enabling
            // filters or other similar requirements a plugin might need in order to
            // be enabled and provide the tag it supports. For now, we assume such
            // configuration cannot be modified programmatically.
            if ($definition->hasConditions()) {
                continue;
            }
            // True if a plugin has already been selected. If another plugin
            // supports $tag, it will be compared against this one. Whichever
            // provides broader support for $tag will be the plugin id returned by
            // this method.
            $selected_plugin = isset($selected_provided_elements[$tag]);
            $selected_config = $selected_provided_elements[$tag] ?? FALSE;
            // True if a plugin supporting $tag has been selected but does not allow
            // any attributes while the plugin currently being checked does support
            // attributes.
            $adds_attribute_config = is_array($provided_elements[$tag]) && $selected_plugin && !is_array($selected_config);
            $broader_attribute_config = FALSE;
            // If the selected plugin and the plugin being checked both have arrays
            // for $tag configuration, they both have attribute configuration. Check
            // which attribute configuration is more permissive.
            if ($selected_plugin && is_array($selected_config) && is_array($provided_elements[$tag])) {
                $selected_plugin_full_attributes = array_filter($selected_config, function ($attribute_config) {
                    return !is_array($attribute_config);
                });
                $being_checked_plugin_full_attributes = array_filter($provided_elements[$tag], function ($attribute_config) {
                    return !is_array($attribute_config);
                });
                if (count($being_checked_plugin_full_attributes) > count($selected_plugin_full_attributes)) {
                    $broader_attribute_config = TRUE;
                }
            }
            if (empty($selected_provided_elements) || $broader_attribute_config || $adds_attribute_config) {
                $selected_provided_elements = $provided_elements;
                $plugin_id = $id;
            }
        }
    }
    return $plugin_id;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.