You are here

message_subscribe_ui.module in Message Subscribe 8

Same filename and directory in other branches
  1. 7 message_subscribe_ui/message_subscribe_ui.module

Hook implementations for the Message Subscribe UI module.

File

message_subscribe_ui/message_subscribe_ui.module
View source
<?php

/**
 * @file
 * Hook implementations for the Message Subscribe UI module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\flag\FlagInterface;
use Drupal\views\Views;

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function message_subscribe_ui_form_flag_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $prefix = \Drupal::config('message_subscribe.settings')
    ->get('flag_prefix');

  /** @var \Drupal\flag\FlagInterface $flag */
  $flag = $form_state
    ->getFormObject()
    ->getEntity();
  if (strpos($flag
    ->id(), $prefix) === 0) {

    // This is a message subscribe flag, so add the view selection option.
    $form['message_subscribe_view_name'] = [
      '#type' => 'select',
      '#title' => t('View to use for the Message Subscription UI'),
      '#description' => t('This view will be used for users to manage their message subscriptions.'),
      '#options' => Views::getViewsAsOptions(),
      '#default_value' => $flag
        ->getThirdPartySetting('message_subscribe_ui', 'view_name', $prefix . '_' . $flag
        ->getFlaggableEntityTypeId() . ':default'),
      '#required' => TRUE,
    ];

    // Add an entity builder to save/set the option.
    $form['#entity_builders'][] = 'message_subscribe_ui_form_flag_form_builder';
  }
}

/**
 * Entity builder for the flag form.
 *
 * @see message_subscribe_ui_form_flag_form_alter()
 */
function message_subscribe_ui_form_flag_form_builder($entity_type, FlagInterface $flag, array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->hasValue('message_subscribe_view_name')) {
    $flag
      ->setThirdPartySetting('message_subscribe_ui', 'view_name', $form_state
      ->getValue('message_subscribe_view_name'));
  }
}