You are here

class LinkitDrupalLink in Linkit 8.5

Adds a settings form to select a Linkit profile on the default link plugin.

Hierarchy

Expanded class hierarchy of LinkitDrupalLink

File

src/Plugin/CKEditorPlugin/LinkitDrupalLink.php, line 16

Namespace

Drupal\linkit\Plugin\CKEditorPlugin
View source
class LinkitDrupalLink extends DrupalLink implements CKEditorPluginConfigurableInterface, ContainerFactoryPluginInterface {

  /**
   * The Linkit profile storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $linkitProfileStorage;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $linkit_profile_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->linkitProfileStorage = $linkit_profile_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager')
      ->getStorage('linkit_profile'));
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
    $settings = $editor
      ->getSettings();
    $all_profiles = $this->linkitProfileStorage
      ->loadMultiple();
    $options = [];
    foreach ($all_profiles as $profile) {
      $options[$profile
        ->id()] = $profile
        ->label();
    }
    $form['linkit_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Linkit enabled'),
      '#default_value' => isset($settings['plugins']['drupallink']['linkit_enabled']) ? $settings['plugins']['drupallink']['linkit_enabled'] : '',
      '#description' => $this
        ->t('Enable Linkit for this text format.'),
    ];
    $form['linkit_profile'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Linkit profile'),
      '#options' => $options,
      '#default_value' => isset($settings['plugins']['drupallink']['linkit_profile']) ? $settings['plugins']['drupallink']['linkit_profile'] : '',
      '#empty_option' => $this
        ->t('- Select -'),
      '#description' => $this
        ->t('Select the Linkit profile you wish to use with this text format.'),
      '#states' => [
        'invisible' => [
          'input[data-drupal-selector="edit-editor-settings-plugins-drupallink-linkit-enabled"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
      '#element_validate' => [
        [
          $this,
          'validateLinkitProfileSelection',
        ],
      ],
    ];
    return $form;
  }

  /**
   * Linkit profile select validation.
   *
   * #element_validate callback for the "linkit_profile" element.
   *
   * @param array $element
   *   An associative array containing the properties and children of the
   *   generic form element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form for the form this element belongs to.
   *
   * @see \Drupal\Core\Render\Element\FormElement::processPattern()
   */
  public function validateLinkitProfileSelection(array $element, FormStateInterface $form_state) {
    $values = $form_state
      ->getValue([
      'editor',
      'settings',
      'plugins',
      'drupallink',
    ]);
    $enabled = isset($values['linkit_enabled']) && $values['linkit_enabled'] === 1;
    if ($enabled && empty(trim($values['linkit_profile']))) {
      $form_state
        ->setError($element, $this
        ->t('Please select the Linkit profile you wish to use.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CKEditorPluginBase::getDependencies public function Returns a list of plugins this plugin requires. Overrides CKEditorPluginInterface::getDependencies 1
CKEditorPluginBase::isInternal public function Indicates if this plugin is part of the optimized CKEditor build. Overrides CKEditorPluginInterface::isInternal 4
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DrupalLink::getButtons public function Returns the buttons that this plugin provides, along with metadata. Overrides CKEditorPluginButtonsInterface::getButtons
DrupalLink::getConfig public function Returns the additions to CKEDITOR.config for a specific CKEditor instance. Overrides CKEditorPluginInterface::getConfig
DrupalLink::getFile public function Returns the Drupal root-relative file path to the plugin JavaScript file. Overrides CKEditorPluginInterface::getFile
DrupalLink::getLibraries public function Returns a list of libraries this plugin requires. Overrides CKEditorPluginBase::getLibraries
LinkitDrupalLink::$linkitProfileStorage protected property The Linkit profile storage.
LinkitDrupalLink::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
LinkitDrupalLink::settingsForm public function Returns a settings form to configure this CKEditor plugin. Overrides CKEditorPluginConfigurableInterface::settingsForm
LinkitDrupalLink::validateLinkitProfileSelection public function Linkit profile select validation.
LinkitDrupalLink::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.