visualn_embed.module

Contains visualn_embed.module.

File

modules/visualn_embed/visualn_embed.module
View source
<?php

/**
 * @file
 * Contains visualn_embed.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\visualn_embed\Controller\DrawingActionsController;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Template\Attribute;

/**
 * Implements hook_help().
 */
function visualn_embed_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the visualn_embed module.
    case 'help.page.visualn_embed':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Ckeditor integration for VisualN.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_theme().
 */
function visualn_embed_theme($existing, $type, $theme, $path) {
  return [
    'visualn_embed_drawing' => [
      'variables' => [
        'id' => '',
        'label' => '',
        'has_access' => true,
        'fetcher_field' => '',
        'fetcher_plugin' => '',
        'properties' => [
          'align' => '',
          'width' => '',
          'height' => '',
        ],
      ],
    ],
    // drawings list dialog form
    'visualn_embed_drawing_select_dialog' => [
      'render element' => 'form',
    ],
    'visualn_embed_drawing_select_item_label' => [
      'variables' => [
        'name' => '',
        'id' => '',
        'thumbnail_path' => '',
        'preview_link' => '',
        'edit_link' => '',
        'delete_link' => '',
      ],
    ],
    // drawing types links with thumbnails wrapper
    'visualn_embed_new_drawing_type_select_links' => [
      'variables' => [
        'items' => '',
      ],
    ],
    'visualn_embed_new_drawing_type_select_item_label' => [
      'variables' => [
        'name' => '',
        'id' => '',
        'thumbnail_path' => '',
        'description' => '',
      ],
    ],
  ];
}

/**
 * Implements hook_preprocess_pager().
 */
function visualn_embed_preprocess_pager(&$variables) {

  // @todo: the hook is called for every pager on the site though needed only
  //   for one specific pager
  // see DrawingEmbedListDialogForm::processDrawingsOptionsList()
  if (empty($variables['pager']['#visualn_embed_pager'])) {
    return;
  }

  // add ajax behaviour to dialog pager links
  $attributes = new Attribute([
    'class' => [
      'use-ajax',
    ],
  ]);
  if (!empty($variables['items']['pages'])) {
    foreach ($variables['items']['pages'] as $k => $item) {
      $variables['items']['pages'][$k]['attributes'] = $attributes;
    }
  }
  foreach ([
    'first',
    'previous',
    'next',
    'last',
  ] as $key) {
    if (!empty($variables['items'][$key])) {
      $variables['items'][$key]['attributes'] = $attributes;
    }
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function visualn_embed_form_visualn_drawing_confirm_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_state
    ->get('visualn_drawing_preview_dialog')) {

    // @todo: attach #ajax behaviour to close the modal to the Cancel link instead of hiding it
    //   or even attach a js handler to close the modal on click
    unset($form['actions']['cancel']);
    $form['actions']['submit']['#ajax'] = [
      // @todo: rename method called to ajaxCloseDialog (and maybe refresh the list of drawings)
      'callback' => [
        DrawingActionsController::class,
        'ajaxGoToListCallback',
      ],
      'event' => 'click',
    ];
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function visualn_embed_form_visualn_drawing_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // if ($form_state->getBuildInfo()['base_form_id']) == 'visualn_drawing_form'

  //dsm($form_state->getBuildInfo()['base_form_id']);

  // @todo: also, when adding form via standard entity interface,
  //   form_id is "visualn_drawing_custom_drawing_add_form" (maybe because of 'default' action instead of 'add')

  //if ($form_id == 'visualn_drawing_custom_drawing_form') {

  // check if the form is opened in ckeditor modal, otherwise return
  if ($form_state
    ->get('visualn_drawing_preview_dialog')) {

    // @todo: also show status messages after saving, otherwise they will be shown after page refresh
    $callback = [
      DrawingActionsController::class,
      'ajaxGoToListCallback',
    ];
    if ($form_state
      ->get('visualn_update_widget')) {
      $callback = [
        DrawingActionsController::class,
        'ajaxUpdateCallback',
      ];
      $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
    }
    $form['actions']['submit']['#ajax'] = [
      'callback' => $callback,
      //'callback' => [DrawingActionsController::class, 'ajaxGoToListCallback'],
      'event' => 'click',
    ];

    // Add an additional 'Save and embed' submit to the newly created drawings forms (opened in modal dialog)
    $entity = $form_state
      ->getFormObject()
      ->getEntity();

    // actually no need to check visualn_update_widget since it can't be updated
    // from ckeditor context menu if the drawing is new
    if ($entity
      ->isNew() && !$form_state
      ->get('visualn_update_widget')) {
      $form['actions']['submit_embed'] = [
        '#value' => t('Save and embed'),
      ] + $form['actions']['submit'];

      // set a different callback to close all dialogs and embed the new drawing
      $form['actions']['submit_embed']['#ajax']['callback'] = [
        DrawingActionsController::class,
        'ajaxSaveEmbedCallback',
      ];
    }

    // @todo: convert link to #ajax instead of hiding it
    if (isset($form['actions']['delete'])) {
      unset($form['actions']['delete']);
    }

    // @see EntityForm::actions()
    // @todo: maybe add 'Back to the types list' button to return to drawing type select dialog
    // @todo: also if needed, add an additional submit to add FormState::disableRedirect()
    //   which is set in VisualNDrawingForm::save(), though it doen't have effect on ajax
    //   so not needed for now
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Add visualn_embed drawings specific iframes configuration settings.
 * Use 'visualn_iframe_configuration' form_id.
 * @see \Drupal\visualn_iframe\Form::ConfigurationForm
 *
 * @ingroup iframes_toolkit
 */
function visualn_embed_form_visualn_iframe_configuration_alter(&$form, FormStateInterface $form_state, $form_id) {
  $config = \Drupal::config('visualn_embed.iframe.settings');
  $form['visualn_embed'] = [
    '#type' => 'details',
    '#title' => 'VisualN Embed',
    '#open' => FALSE,
  ];

  // @todo: review key names
  // @todo: use the setting in Embedded Drawing properties form
  $form['visualn_embed']['allow_drawings_sharing'] = [
    '#type' => 'checkbox',
    '#title' => t('Allow Drawings sharing'),
    '#default_value' => $config
      ->get('allow_drawings_sharing'),
    '#description' => t('Allow sharing for VisualN drawings. Already shared iframes will still be available even if sharing disabled.'),
  ];

  // @todo: add a setting to hide/show exposed share links that wouldn't depend on the setting above
  $form['visualn_embed']['implicit_entries_restore'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable missing iframe entries implicit restore'),
    '#default_value' => $config
      ->get('implicit_entries_restore'),
    '#description' => t('Recreate VisualN embed drawings on drawing view if an iframe entry doesn\'t exist. Useful when settings embed settings manually. <strong>Should be used only when needed and commonly disabled</strong>.'),
  ];

  // @todo: maybe use array_unshift to execute it first and remove its values
  $form['#submit'][] = 'visualn_embed_iframe_configuration_form_submit';
}
function visualn_embed_iframe_configuration_form_submit($form, FormStateInterface $form_state) {
  $config = \Drupal::service('config.factory')
    ->getEditable('visualn_embed.iframe.settings');
  $values = $form_state
    ->getValue('visualn_embed');

  // @todo: maybe use setData() instead to override whole config
  //   though don't override 'dependencies' key
  foreach ($values as $key => $value) {
    $config
      ->set($key, $value);
  }
  $config
    ->save();

  // The cache tag attached in EmbedDrawingFilter::process() to embedded
  // drawings share links to reset the cache and show/hide links
  // on allow_drawings_sharing setting change.
  // See the same implementation for the visualn_block case.
  // @todo: maybe also use more specific cache tags,
  //   e.g. visualn_embed_iframe_settings:setting_name
  //   or visualn_embed:iframe_settings:setting_name
  //   or visualn_embed.iframe_settings:setting_name
  $tags = [
    'visualn_embed_iframe_settings',
  ];
  Cache::invalidateTags($tags);
}

Functions

Namesort descending Description
visualn_embed_form_visualn_drawing_confirm_form_alter Implements hook_form_BASE_FORM_ID_alter().
visualn_embed_form_visualn_drawing_form_alter Implements hook_form_BASE_FORM_ID_alter().
visualn_embed_form_visualn_iframe_configuration_alter Implements hook_form_FORM_ID_alter().
visualn_embed_help Implements hook_help().
visualn_embed_iframe_configuration_form_submit
visualn_embed_preprocess_pager Implements hook_preprocess_pager().
visualn_embed_theme Implements hook_theme().