subscriptions-2.0.x-dev/subscriptions_ui/subscriptions_ui.module

subscriptions_ui/subscriptions_ui.module
<?php

/**
 * @file
 * Provides a user interface for Subscriptions.
 */

use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adds the Display Settings part to the admin/settings/subscriptions form.
 *
 * @param array $form
 * @param Drupal\Core\Form\FormStateInterface $form_state
 * @param string $form_id
 *
 * @ingroup hooks
 * @ingroup form
 */
function subscriptions_ui_form_subscriptions_settings_alter(array &$form, FormStateInterface $form_state, string $form_id) {
  $config = \Drupal::config('subscriptions_ui.settings');
  $link_only = $config->get('link_only');
  $expand = $config->get('form_expanded');
  $show_by_author_options = $config->get('show_by_author_options');
  $tr = 't';

  $form['display_settings'] = [
    '#type' => 'fieldset',
    '#title' => t('Display settings'),
    '#collapsible' => TRUE,
    '#weight' => 1,
  ];
  $url = Url::fromRoute('block.admin_display');
  $link = Link::fromTextAndUrl(t('here'), $url)->toString();
  $description = t('How to display the subscriptions sub-form on node pages.  Default is the first option.<br />To use the block, <b>you must enable the block</b> @here; put it into the %content region and set the %block_title to @none.', [
    '@here' => $link,
    '%content' => 'content',
    '%block_title' => $tr('Block title'),
    '@none' => '<em>&lt;none&gt;</em>',
  ]);
  $form['display_settings']['subscriptions_form_link_only'] = [
    '#type' => 'radios',
    '#title' => t('Node form visibility'),
    '#options' => [
      t('Always display the fieldset'),
      t('Display only a @subscribe link that makes the fieldset visible', ['@subscribe' => t('Subscribe')]),
    ],
    '#default_value' => $link_only,
    '#description' => t('What to display.  Default is the first option.'),
  ];
  $form['display_settings']['subscriptions_form_expanded'] = [
    '#type' => 'checkbox',
    '#title' => t('Expand the node form fieldset'),
    '#default_value' => $expand,
    '#description' => t('Displays the fieldset with the node page subscriptions sub-form in expanded state.  Default is OFF.'),
  ];
  $form['display_settings']['note'] = [
    '#markup' => '<p>' . t("Note: Our <b>favorite display settings</b> are the exact opposites of the defaults, but we chose the defaults, because they work without enabling the Subscriptions block.") . '</p>',
  ];
  $form['display_settings']['subscriptions_show_by_author_options'] = [
    '#type' => 'checkbox',
    '#title' => t("Show 'by author' subscriptions options"),
    '#default_value' => $show_by_author_options,
    '#description' => t("If you don't want your users to subscribe 'by author', then turn this off.  Default is ON."),
  ];
}

/**
 * Asks for permission to display the subscriptions interface
 * for the given node.
 *
 * This should be used as follows:
 *   if (module_invoke('subscriptions_ui', 'get_permission_to_handle', $nid,
 * 'mymodule') !== FALSE) { my_module_display_interface($nid);
 *   }
 * and mymodule needs to implement hook_subscriptions_ui(), see below.
 *
 * @param int $nid
 * @param string $module
 *
 * @return bool
 */
function subscriptions_ui_get_permission_to_handle(int $nid, string $module): bool {
  $subscribe_to_content = \Drupal::currentUser()->hasPermission('subscribe to content');
  if (subscriptions_node_is_blocked($nid) || !$subscribe_to_content) {
    return FALSE;
  }

  static $permissions = [];
  if (empty($permissions[$nid])) {
    foreach (\Drupal::moduleHandler()->getImplementations('subscriptions_ui') as $m) {
      $perm = \Drupal::moduleHandler()->invoke($m, 'subscriptions_ui', $nid);
      if (empty($permissions[$nid]) || $permissions[$nid]['priority'] < $perm['priority']) {
        $permissions[$nid] = $perm;
      }
    }
  }
  return $permissions[$nid]['module'] == $module;
}

/**
 * Implements hook_subscriptions_ui().
 *
 * subscriptions_ui is willing to handle all $nids.
 * Other modules can return a higher priority with their name
 * (or a different name!) depending on the $nid, $user, etc.
 *
 * @param int $nid
 *
 * @return array
 */
function subscriptions_ui_subscriptions_ui(int $nid): array {
  return [
    'priority' => 0,
    'module' => 'subscriptions_ui',
  ];
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc