You are here

public function IPRangesFormController::form in IP Ranges 8

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

lib/Drupal/ip_ranges/IPRangesFormController.php, line 24

Class

IPRangesFormController

Namespace

Drupal\ip_ranges

Code

public function form(array $form, array &$form_state) {
  $entity = $this->entity;

  // Show whitelist your own IP link only on Add IP Range form.
  if ($this->operation == 'add') {
    $form['own_ip_warning'] = array(
      '#type' => 'value',
      '#markup' => t('<strong>Note that your own IP Address is currently @ip. Be careful not to lock yourself out!</strong>', array(
        '@ip' => $this->ownIp,
      )) . '<br />' . l(t('Click here to whitelist your own IP-address.'), '', array(
        'fragment' => '',
        'external' => TRUE,
        'attributes' => array(
          'id' => 'add-my-own-ip',
          'data-my-ip' => $this->ownIp,
        ),
      )),
    );
  }
  $form['ip_lower'] = array(
    '#title' => t('IP range start / Single IP-address'),
    '#type' => 'textfield',
    '#size' => 48,
    '#required' => TRUE,
    '#maxlength' => 15,
    '#description' => t('Enter IP-address (100.100.100.100). If range end is specified, it will be used as start of the range, otherwise as a single IP-address.'),
  );
  $form['ip_higher'] = array(
    '#title' => t('IP range end (optional)'),
    '#type' => 'textfield',
    '#size' => 48,
    '#required' => FALSE,
    '#maxlength' => 15,
    '#description' => t('If entered, the banned ip will be treated as a range.'),
  );
  $form['type'] = array(
    '#title' => t('List type'),
    '#description' => t('Add the IP to a whitelist to ensure access or the blacklist to deny access'),
    '#type' => 'select',
    '#options' => array(
      0 => t('Blacklist'),
      1 => t('Whitelist'),
    ),
    '#multiple' => FALSE,
    '#default_value' => 0,
    '#required' => TRUE,
  );
  $form['description'] = array(
    '#title' => t('IP Range description (optional)'),
    '#type' => 'textfield',
    '#size' => 68,
    '#required' => FALSE,
    '#maxlength' => 254,
    '#description' => t('Description can be used to better identify added IP Ranges'),
  );

  // Default values for edit form.
  if ($this->operation == 'edit') {
    $ip_lower = $entity
      ->getIpLower();
    $ip_higher = $entity
      ->getIpHigher();
    $form['type']['#default_value'] = $entity
      ->getType();
    $form['ip_lower']['#default_value'] = long2ip($ip_lower);

    // Only show higher IP if its different than lower.
    if ($ip_lower != $ip_higher) {
      $form['ip_higher']['#default_value'] = long2ip($ip_higher);
    }
    $form['description']['#default_value'] = $entity
      ->getDescription();
  }

  // Attach form javascript.
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'ip_ranges') . '/js/ip_ranges_form.js',
  );
  return parent::form($form, $form_state, $entity);
}