AnchorLink.php in CKEditor Anchor Link - For Drupal 8 and 9 8.2
File
src/Plugin/CKEditorPlugin/AnchorLink.php
View source
<?php
namespace Drupal\anchor_link\Plugin\CKEditorPlugin;
use Drupal\editor\Entity\Editor;
use Drupal\ckeditor\CKEditorPluginBase;
class AnchorLink extends CKEditorPluginBase {
public function getFile() {
return $this
->getLibraryPath() . '/plugin.js';
}
public function getDependencies(Editor $editor) {
return [
'fakeobjects',
];
}
public function getLibraries(Editor $editor) {
return [];
}
public function isInternal() {
return FALSE;
}
public function getButtons() {
$libraryUrl = $this
->getLibraryUrl();
return [
'Link' => [
'label' => $this
->t('Link'),
'image' => $libraryUrl . '/icons/link.png',
],
'Unlink' => [
'label' => $this
->t('Unlink'),
'image' => $libraryUrl . '/icons/unlink.png',
],
'Anchor' => [
'label' => $this
->t('Anchor'),
'image' => $libraryUrl . '/icons/anchor.png',
],
];
}
public function getConfig(Editor $editor) {
return [];
}
protected function getLibraryPath() {
$directories[] = \Drupal::service('site.path') . "/libraries/";
$directories[] = 'libraries/';
if ($installProfile = \Drupal::installProfile()) {
$profile_path = drupal_get_path('profile', $installProfile);
$directories[] = "{$profile_path}/libraries/";
}
foreach ($directories as $dir) {
if (file_exists(DRUPAL_ROOT . '/' . $dir . 'link/plugin.js')) {
return $dir . 'link';
}
}
return 'libraries/link';
}
protected function getLibraryUrl() {
$originUrl = \Drupal::request()
->getSchemeAndHttpHost() . \Drupal::request()
->getBaseUrl();
$librarayPath = DRUPAL_ROOT . '/libraries/link';
$librarayUrl = $originUrl . '/libraries/link';
$libraryFound = file_exists($librarayPath . '/plugin.js');
if (!$libraryFound) {
if ($installProfile = \Drupal::installProfile()) {
$profilePath = drupal_get_path('profile', $installProfile);
$profilePath .= '/libraries/link';
if (file_exists(DRUPAL_ROOT . '/' . $profilePath . '/plugin.js')) {
$libraryFound = TRUE;
$librarayUrl = $originUrl . '/' . $profilePath;
}
}
}
if ($libraryFound) {
return $librarayUrl;
}
else {
return $originUrl . '/libraries/link';
}
}
}