linkit.module 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @file
  4. *
  5. */
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Routing\RouteMatchInterface;
  8. use Drupal\Core\StreamWrapper\StreamWrapperInterface;
  9. use Drupal\Core\Url;
  10. use Drupal\linkit\ProfileInterface;
  11. /**
  12. * Implements hook_help().
  13. */
  14. function linkit_help($route_name, RouteMatchInterface $route_match) {
  15. switch ($route_name) {
  16. case 'entity.linkit_profile.attributes':
  17. return '<p>' . t('Attributes are HTML attributes that will be attached to the insert plugin.') . '</p>';
  18. break;
  19. }
  20. }
  21. /**
  22. * Implements hook_form_BASE_FORM_ID_alter() for linkit_profile_form on behalf
  23. * of the 'imce' module.
  24. *
  25. * Adds IMCE settings to the form.
  26. *
  27. * @see imce_form_linkit_profile_form_builder()
  28. */
  29. function imce_form_linkit_profile_form_alter(&$form, FormStateInterface $form_state) {
  30. /** @var \Drupal\Linkit\ProfileInterface $linkit_profile */
  31. $linkit_profile = $form_state->getFormObject()->getEntity();
  32. $form['imce'] = array(
  33. '#type' => 'details',
  34. '#title' => t('IMCE integration'),
  35. '#group' => 'additional_settings',
  36. );
  37. $form['imce']['imce_use'] = array(
  38. '#type' => 'checkbox',
  39. '#title' => t('Enable IMCE File Browser in the editor dialog.'),
  40. '#default_value' => $linkit_profile->getThirdPartySetting('imce', 'use', FALSE),
  41. );
  42. $scheme_options = \Drupal::service('stream_wrapper_manager')->getNames(StreamWrapperInterface::READ_VISIBLE);
  43. $form['imce']['imce_scheme'] = array(
  44. '#type' => 'radios',
  45. '#title' => t('Scheme'),
  46. '#options' => $scheme_options,
  47. '#default_value' => $linkit_profile->getThirdPartySetting('imce', 'scheme', 'public'),
  48. '#states' => [
  49. 'visible' => [
  50. ':input[name="imce_use"]' => ['checked' => TRUE],
  51. ],
  52. ],
  53. );
  54. $form['#entity_builders'][] = 'imce_form_linkit_profile_form_builder';
  55. }
  56. /**
  57. * Entity builder for the linkit profile form with imce options.
  58. *
  59. * @see imce_form_linkit_profile_form_alter().
  60. */
  61. function imce_form_linkit_profile_form_builder($entity_type, ProfileInterface $linkit_profile, &$form, FormStateInterface $form_state) {
  62. $linkit_profile->setThirdPartySetting('imce', 'use', $form_state->getValue('imce_use'));
  63. $linkit_profile->setThirdPartySetting('imce', 'scheme', $form_state->getValue('imce_scheme'));
  64. }
  65. /**
  66. * Implements hook_form_BASE_FORM_ID_alter() for linkit_editor_dialog_form on
  67. * behalf of the 'imce' module.
  68. *
  69. * Adds a button to open the imce file browser if it is enabled.
  70. */
  71. function imce_form_linkit_editor_dialog_form_alter(&$form, FormStateInterface $form_state) {
  72. /** @var \Drupal\Linkit\ProfileInterface $linkit_profile */
  73. $linkit_profile = $form_state->getFormObject()->getLinkitProfile();
  74. if($linkit_profile->getThirdPartySetting('imce', 'use', FALSE)) {
  75. $form['imce-link'] = [
  76. '#type' => 'link',
  77. '#title' => t('Open IMCE file browser'),
  78. '#url' => Url::fromRoute('imce.page', [
  79. 'scheme' => $linkit_profile->getThirdPartySetting('imce', 'scheme', 'public'),
  80. ]),
  81. '#options' => array(
  82. 'query' => array(
  83. 'sendto' => 'linkitImce.sendto',
  84. ),
  85. ),
  86. '#attributes' => [
  87. 'class' => ['linkit-imce-open'],
  88. ],
  89. '#attached' => [
  90. 'library' => [
  91. 'linkit/linkit.imce'
  92. ],
  93. ],
  94. '#weight' => 1,
  95. ];
  96. }
  97. }