You are here

public function CommentNotifySettings::buildForm in Comment Notify 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/CommentNotifySettings.php, line 85

Class

CommentNotifySettings
Settings form for the Comment Notify module.

Namespace

Drupal\comment_notify\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('comment_notify.settings');
  $bundle_checkboxes = $this
    ->getCommentFieldIdentifiers();

  // Only perform comment_notify for certain bundle types.
  $enabled_bundles = $config
    ->get('bundle_types');
  $anonymous_problems = [];

  // If they don't have the ability to leave contact info, then we make a
  // report.
  $entity_types = [];
  foreach ($bundle_checkboxes as $comment_field_identifier => $bundle_checkbox_label) {
    $comment_field_info = explode('--', $comment_field_identifier);
    $entity_type = $comment_field_info[0];
    $entity_bundle = $comment_field_info[1];
    $field_name = $comment_field_info[2];
    $entity_types[$entity_type][] = $comment_field_identifier;
    $comment_field = FieldConfig::loadByName($entity_type, $entity_bundle, $field_name);
    if (in_array($entity_type . '--' . $entity_bundle . '--' . $field_name, $enabled_bundles) && $comment_field && $comment_field
      ->getSetting('anonymous') == CommentInterface::ANONYMOUS_MAYNOT_CONTACT) {
      if (User::getAnonymousUser()
        ->hasPermission('subscribe to comments')) {

        // Provide a link if the field_ui module is installed.
        if ($this->moduleHandler
          ->moduleExists('field_ui')) {
          $link = Link::fromTextAndUrl($comment_field_identifier, $comment_field
            ->toUrl($entity_type . '-field-edit-form'));
          $no_allowed_contact_info_field[] = $link
            ->toString();
        }
        else {
          $no_allowed_contact_info_field[] = $comment_field_identifier;
        }
      }
    }
  }

  // If anonymous users can subscribe to comments they must be allowed to
  // post comments and leave their contact information.
  if (User::getAnonymousUser()
    ->hasPermission('subscribe to comments')) {
    if (!User::getAnonymousUser()
      ->hasPermission('post comments')) {
      $anonymous_problems = $this
        ->t('Post comments');
    }
    elseif (!empty($no_allowed_contact_info_field)) {
      $markup = new Markup();
      $fields = $markup
        ->create(implode('</li><li>', $no_allowed_contact_info_field));
      $anonymous_problems = $this
        ->t("Leave their contact information on the following fields: <ul><li>@fields</li></ul>", [
        '@fields' => $fields,
      ]);
    }
    if (!empty($anonymous_problems)) {
      $this->messenger
        ->addWarning($this
        ->t('Anonymous commenters have the permission to subscribe to comments but they need to be allowed to:<br/>@anonymous_problems', [
        '@anonymous_problems' => $anonymous_problems,
      ]));
    }
  }
  $form['bundle_types'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Bundles to enable for comment notification'),
    '#default_value' => $enabled_bundles,
    '#options' => $bundle_checkboxes,
    '#description' => $this
      ->t('Comments on bundle types enabled here will have the option of comment notification. Written as "Entity Type: Bundle: Comment field".'),
  ];
  $form['available_alerts'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Available subscription modes'),
    '#return_value' => 1,
    '#default_value' => array_keys(array_filter($config
      ->get('available_alerts'))),
    '#description' => $this
      ->t('Choose which notification subscription styles are available for users'),
    '#options' => [
      COMMENT_NOTIFY_ENTITY => $this
        ->t('All comments'),
      COMMENT_NOTIFY_COMMENT => $this
        ->t('Replies to my comment'),
    ],
  ];
  $available_options[COMMENT_NOTIFY_DISABLED] = $this
    ->t('No notifications');
  $available_options += _comment_notify_options();
  $form['enable_default'] = [
    '#type' => 'container',
    '#tree' => TRUE,
  ];
  $form['enable_default']['watcher'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default state for the notification selection box'),
    '#return_value' => 1,
    '#default_value' => $config
      ->get('enable_default.watcher'),
    '#description' => $this
      ->t('This flag presets the flag for the follow-up notification on the form that users will see when posting a comment'),
    '#options' => $available_options,
  ];
  $form['enable_default']['entity_author'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Subscribe users to their entity follow-up notification emails by default'),
    '#default_value' => $config
      ->get('enable_default.entity_author'),
    '#description' => $this
      ->t('If this is checked, new users will receive e-mail notifications for follow-ups on their entities by default until they individually disable the feature.'),
  ];
  $form['mail_templates'] = [
    '#type' => 'container',
    '#tree' => TRUE,
  ];
  $form['mail_templates']['watcher'] = [
    '#type' => 'container',
    '#tree' => TRUE,
  ];
  $form['mail_templates']['entity_author'] = [
    '#type' => 'container',
    '#tree' => TRUE,
  ];

  // Create notification options for each supported entity type.
  foreach ($entity_types as $entity_type => $checkboxes) {

    // The #states logic is rather messy. It needs to allow any of the
    // checkboxes for a specific entity type to make the email fields visible
    // so each checkbox has to be part of a jQuery OR selector.
    $checkboxeses = [];
    foreach ($checkboxes as $checkbox) {
      $checkboxeses[] = ':input[name="bundle_types[' . $checkbox . ']"]';
    }
    $checkboxeses = implode(',', $checkboxeses);
    $form['mail_templates']['watcher'][$entity_type] = [
      '#type' => 'container',
      '#tree' => TRUE,
    ];
    $form['mail_templates']['watcher'][$entity_type]['subject'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default mail subject for sending out :entity_type notifications to commenters', [
        ':entity_type' => $entity_type,
      ]),
      '#default_value' => $config
        ->get('mail_templates.watcher.' . $entity_type . '.subject'),
      '#token_types' => [
        'comment',
        'comment-subscribed',
        $entity_type,
      ],
      '#element_validate' => [
        'token_element_validate',
      ],
      '#states' => [
        'visible' => [
          $checkboxeses => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['mail_templates']['watcher'][$entity_type]['body'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('%label: Default mail text for sending out notifications to commenters', [
        '%label' => $entity_type,
      ]),
      '#default_value' => $config
        ->get('mail_templates.watcher.' . $entity_type . '.body'),
      '#cols' => 80,
      '#rows' => 15,
      // @todo Change from 'node' to 'entity'.
      // See Issue #1061750 on Drupal.org
      '#token_types' => [
        'comment',
        'comment-subscribed',
        $entity_type,
      ],
      '#element_validate' => [
        'token_element_validate',
      ],
      '#states' => [
        'visible' => [
          $checkboxeses => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['mail_templates']['entity_author'][$entity_type] = [
      '#type' => 'container',
      '#tree' => TRUE,
    ];
    $form['mail_templates']['entity_author'][$entity_type]['subject'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default mail subject for sending out :entity_type notifications to authors', [
        ':entity_type' => $entity_type,
      ]),
      '#default_value' => $config
        ->get('mail_templates.entity_author.' . $entity_type . '.subject'),
      '#token_types' => [
        'comment',
        $entity_type,
        'user',
      ],
      '#element_validate' => [
        'token_element_validate',
      ],
      '#states' => [
        'visible' => [
          $checkboxeses => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['mail_templates']['entity_author'][$entity_type]['body'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('%label: Default mail text for sending out the notifications to entity authors', [
        '%label' => $entity_type,
      ]),
      '#default_value' => $config
        ->get('mail_templates.entity_author.' . $entity_type . '.body'),
      '#cols' => 80,
      '#rows' => 15,
      // @todo: Change token from 'node' to 'entity'
      // See Issue #1061750 on Drupal.org
      '#token_types' => [
        'comment',
        $entity_type,
        'user',
      ],
      '#element_validate' => [
        'token_element_validate',
      ],
      '#states' => [
        'visible' => [
          $checkboxeses => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  $form['token_help'] = [
    '#theme' => 'token_tree_link',
    '#token_types' => [
      'comment',
      'comment-subscribed',
      'node',
      'user',
    ],
  ];
  return parent::buildForm($form, $form_state);
}