Follow-up to #2821602: Not working this module for drupal 8 version

Allow permanent banning to be checked on the form without entering a date in the date field for the module's d8 dev version.

Comments

Kifah Meeran created an issue. See original summary.

MaskyS’s picture

Priority: Critical » Normal
MaskyS’s picture

Title: Not working this module for drupal 8 version » Allow permanent banning to be checked on the form without entering a date in t
MaskyS’s picture

Title: Allow permanent banning to be checked on the form without entering a date in t » Allow permanent banning to be checked on the form without entering a date in the date field
andyrigby’s picture

andyrigby’s picture

This seems possible, but there is a core issues with using #states and the datetime field, ref https://www.drupal.org/node/2419131

I managed to work around this by applying the core patch in #55 on https://www.drupal.org/node/2419131#comment-12026681 then altering the #states and #required state on the expiry field using a form alter:

/**
 * Implements hook_form_alter().
 */
function MY_MODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == "user_restrictions_edit_form" || $form_id == "user_restrictions_add_form") {
    $form['expiration']['expiry_container']['expiry']['#states']['required'] = [
      'input[name="permanent"]' => ["checked" => FALSE],
    ];

    $form['expiration']['expiry_container']['expiry']['#required'] = FALSE;

    array_unshift(
      $form['#validate'],
      "_MY_MODULE_restriction_form_validate"
    );
  }
}

function _MY_MODULE_restriction_form_validate($form, FormStateInterface $form_state) {
  $form['expiration']['expiry_container']['expiry']['#required'] = !$form_state->getValue("permanent");
}
adammalone’s picture

Status: Active » Closed (duplicate)

I'm closing this issue as it will be solved with #2936400: Massive refactoring / introduce plugins that I'm working on/reviewing currently.