updated linkit contrib module to last beta version
This commit is contained in:
@@ -2,108 +2,152 @@
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Linkit hook implementations.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function linkit_help($route_name, RouteMatchInterface $route_match) {
|
||||
switch ($route_name) {
|
||||
case 'entity.linkit_profile.attributes':
|
||||
return '<p>' . t('Attributes are HTML attributes that will be attached to the insert plugin.') . '</p>';
|
||||
break;
|
||||
case 'help.page.linkit':
|
||||
$output = '';
|
||||
$output .= '<h3>' . t('About') . '</h3>';
|
||||
$output .= '<p>' . t('The Linkit module provides an easy interface for internal and external linking with wysiwyg editors by using an autocomplete field.') . '</p>';
|
||||
$output .= '<h3>' . t('Uses') . '</h3>';
|
||||
$output .= '<dl>';
|
||||
$output .= '<dt>' . t('Managing Linkit profiles') . '</dt>';
|
||||
$output .= '<dd>' . t('You can create and edit Linkit profiles on the <a href=":profiles">Linkit profile page</a>. You can create a Linkit profile by clicking "<a href=":add_profile">Add profile</a>".', [':profiles' => Url::fromRoute('entity.linkit_profile.collection')->toString(), ':add_profile' => Url::fromRoute('entity.linkit_profile.add_form')->toString()]) . '</dd>';
|
||||
$output .= '</dl>';
|
||||
return $output;
|
||||
|
||||
case 'entity.linkit_profile.collection':
|
||||
$output = '<p>' . t('Linkit profiles define how Linkit will operate on fields that have Linkit attached.') . '</p>';
|
||||
$output .= '<p>' . t('The most common way to use Linkit is to enable Linkit on the Drupal Link plugin and associate a Linkit profile to it on a Text format.') . '</p>';
|
||||
return $output;
|
||||
|
||||
case 'linkit.matchers':
|
||||
$output = '<p>' . t('Matchers defines how different data can be queried and displayed in the autocomplete suggestion list. Multiple matchers of the same type can be used at the same time to granulate the suggestions. The order of the added matchers defines in which order the suggestions will be presented.') . '</p>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_ckeditor_plugin_info_alter().
|
||||
*/
|
||||
function linkit_ckeditor_plugin_info_alter(array &$plugins) {
|
||||
if (isset($plugins['drupallink'])) {
|
||||
$plugins['drupallink']['class'] = "Drupal\\linkit\\Plugin\\CKEditorPlugin\\LinkitDrupalLink";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_BASE_FORM_ID_alter() for linkit_profile_form on behalf
|
||||
* of the 'imce' module.
|
||||
*
|
||||
* Adds IMCE settings to the form.
|
||||
*
|
||||
* @see imce_form_linkit_profile_form_builder()
|
||||
* Implements hook_form_FORM_ID_alter().
|
||||
*/
|
||||
function imce_form_linkit_profile_form_alter(&$form, FormStateInterface $form_state) {
|
||||
/** @var \Drupal\Linkit\ProfileInterface $linkit_profile */
|
||||
$linkit_profile = $form_state->getFormObject()->getEntity();
|
||||
function linkit_form_editor_link_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
|
||||
// Alter only the form with ID 'editor_link_dialog'.
|
||||
if ($form_id !== 'editor_link_dialog') {
|
||||
return;
|
||||
}
|
||||
|
||||
$form['imce'] = array(
|
||||
'#type' => 'details',
|
||||
'#title' => t('IMCE integration'),
|
||||
'#group' => 'additional_settings',
|
||||
);
|
||||
/** @var Drupal\filter\Entity\FilterFormat $filter_format */
|
||||
$filter_format = $form_state->getBuildInfo()['args'][0];
|
||||
|
||||
$form['imce']['imce_use'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Enable IMCE File Browser in the editor dialog.'),
|
||||
'#default_value' => $linkit_profile->getThirdPartySetting('imce', 'use', FALSE),
|
||||
);
|
||||
/** @var \Drupal\Core\Entity\EntityStorageInterface $editorStorage */
|
||||
$editorStorage = Drupal::service('entity.manager')->getStorage('editor');
|
||||
|
||||
$scheme_options = \Drupal::service('stream_wrapper_manager')->getNames(StreamWrapperInterface::READ_VISIBLE);
|
||||
$form['imce']['imce_scheme'] = array(
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Scheme'),
|
||||
'#options' => $scheme_options,
|
||||
'#default_value' => $linkit_profile->getThirdPartySetting('imce', 'scheme', 'public'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[name="imce_use"]' => ['checked' => TRUE],
|
||||
],
|
||||
/** @var \Drupal\editor\EditorInterface $editor */
|
||||
$editor = $editorStorage->load($filter_format->id());
|
||||
$plugin_settings = $editor->getSettings()['plugins']['drupallink'];
|
||||
|
||||
// Do not alter the form if Linkit is not enabled for this text format.
|
||||
if (!isset($plugin_settings['linkit_enabled']) || (isset($plugin_settings['linkit_enabled']) && !$plugin_settings['linkit_enabled'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$linkit_profile_id = $editor->getSettings()['plugins']['drupallink']['linkit_profile'];
|
||||
|
||||
if (isset($form_state->getUserInput()['editor_object'])) {
|
||||
$input = $form_state->getUserInput()['editor_object'];
|
||||
$form_state->set('link_element', $input);
|
||||
$form_state->setCached(TRUE);
|
||||
}
|
||||
else {
|
||||
// Retrieve the link element's attributes from form state.
|
||||
$input = $form_state->get('link_element') ?: [];
|
||||
}
|
||||
|
||||
$form['href_dirty_check'] = [
|
||||
'#type' => 'hidden',
|
||||
'#default_value' => isset($input['href']) ? $input['href'] : '',
|
||||
];
|
||||
|
||||
$form['attributes']['href'] = array_merge($form['attributes']['href'], [
|
||||
'#type' => 'linkit',
|
||||
'#description' => t('Start typing to find content.'),
|
||||
'#autocomplete_route_name' => 'linkit.autocomplete',
|
||||
'#autocomplete_route_parameters' => [
|
||||
'linkit_profile_id' => $linkit_profile_id,
|
||||
],
|
||||
);
|
||||
"#weight" => -10,
|
||||
'#default_value' => isset($input['href']) ? $input['href'] : '',
|
||||
]);
|
||||
|
||||
$form['#entity_builders'][] = 'imce_form_linkit_profile_form_builder';
|
||||
}
|
||||
$fields = [
|
||||
'data-entity-type',
|
||||
'data-entity-uuid',
|
||||
'data-entity-substitution',
|
||||
];
|
||||
|
||||
/**
|
||||
* Entity builder for the linkit profile form with imce options.
|
||||
*
|
||||
* @see imce_form_linkit_profile_form_alter().
|
||||
*/
|
||||
function imce_form_linkit_profile_form_builder($entity_type, ProfileInterface $linkit_profile, &$form, FormStateInterface $form_state) {
|
||||
$linkit_profile->setThirdPartySetting('imce', 'use', $form_state->getValue('imce_use'));
|
||||
$linkit_profile->setThirdPartySetting('imce', 'scheme', $form_state->getValue('imce_scheme'));
|
||||
}
|
||||
$form['attributes']["#weight"] = -100;
|
||||
|
||||
/**
|
||||
* Implements hook_form_BASE_FORM_ID_alter() for linkit_editor_dialog_form on
|
||||
* behalf of the 'imce' module.
|
||||
*
|
||||
* Adds a button to open the imce file browser if it is enabled.
|
||||
*/
|
||||
function imce_form_linkit_editor_dialog_form_alter(&$form, FormStateInterface $form_state) {
|
||||
/** @var \Drupal\Linkit\ProfileInterface $linkit_profile */
|
||||
$linkit_profile = $form_state->getFormObject()->getLinkitProfile();
|
||||
|
||||
if($linkit_profile->getThirdPartySetting('imce', 'use', FALSE)) {
|
||||
$form['imce-link'] = [
|
||||
'#type' => 'link',
|
||||
'#title' => t('Open IMCE file browser'),
|
||||
'#url' => Url::fromRoute('imce.page', [
|
||||
'scheme' => $linkit_profile->getThirdPartySetting('imce', 'scheme', 'public'),
|
||||
]),
|
||||
'#options' => array(
|
||||
'query' => array(
|
||||
'sendto' => 'linkitImce.sendto',
|
||||
),
|
||||
),
|
||||
'#attributes' => [
|
||||
'class' => ['linkit-imce-open'],
|
||||
],
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
'linkit/linkit.imce'
|
||||
],
|
||||
],
|
||||
'#weight' => 1,
|
||||
foreach ($fields as $field_name) {
|
||||
$form['attributes'][$field_name] = [
|
||||
'#title' => $field_name,
|
||||
'#type' => 'hidden',
|
||||
'#default_value' => isset($input[$field_name]) ? $input[$field_name] : '',
|
||||
];
|
||||
}
|
||||
|
||||
// Add #submit callback that handles the data-* attributes.
|
||||
array_unshift($form['#submit'], 'linkit_form_editor_link_dialog_submit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the data-* attributes and href replacement when appropriate.
|
||||
*/
|
||||
function linkit_form_editor_link_dialog_submit(array &$form, FormStateInterface $form_state) {
|
||||
$link_element = $form_state->get('link_element');
|
||||
|
||||
$href = $form_state->getValue(['attributes', 'href']);
|
||||
$href_dirty_check = $form_state->getValue(['href_dirty_check']);
|
||||
|
||||
if ($href !== $href_dirty_check) {
|
||||
$form_state->unsetValue(['attributes', 'data-entity-type']);
|
||||
$form_state->unsetValue(['attributes', 'data-entity-uuid']);
|
||||
$form_state->unsetValue(['attributes', 'data-entity-substitution']);
|
||||
}
|
||||
|
||||
$fields = [
|
||||
'href',
|
||||
'data-entity-type',
|
||||
'data-entity-uuid',
|
||||
'data-entity-substitution',
|
||||
];
|
||||
|
||||
foreach ($fields as $field_name) {
|
||||
$value = $form_state->getValue(['attributes', $field_name]);
|
||||
if (empty($value)) {
|
||||
if (!empty($link_element)) {
|
||||
$form_state->setValue(['attributes', $field_name], '');
|
||||
}
|
||||
else {
|
||||
$form_state->unsetValue(['attributes', $field_name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user