You are here

user_name.element.inc in Flexiform 7

Contains class for the User name element.

File

includes/element/user_name.element.inc
View source
<?php

/**
 * @file
 * Contains class for the User name element.
 */

/**
 * Class to add the node title field to a form.
 */
class FlexiformElementUserName extends FlexiformElement {

  /**
   * Return the form element for this FlexiformElement.
   */
  public function form($form, &$form_state, $entity, $language = LANGUAGE_NONE) {
    $parents = $form['#parents'];
    $parents[] = 'name';

    // Support email registration behaviour by optionally creating a hidden
    // element containing an autogenerated user name.
    if (module_exists('email_registration') && (!isset($this->settings['email_registration']) || !empty($this->settings['email_registration']))) {
      $form[$this->element_namespace] = array(
        '#type' => 'hidden',
        '#parents' => $parents,
        '#title' => $this
          ->label(),
        '#value' => !empty($entity->name) ? $entity->name : 'email_registration_' . user_password(),
        '#required' => TRUE,
      );
      return parent::form($form, $form_state, $entity);
    }

    // Work out the default value.
    $default = '';
    if (!empty($this->settings['default_value']['default_value'])) {
      $default = $this->settings['default_value']['default_value'];
    }
    if (!empty($this->settings['default_value']['use_tokens'])) {
      $default = $this
        ->replaceCtoolsSubstitutions($default, $form['#flexiform_entities']);
    }
    $form[$this->element_namespace] = array(
      '#type' => 'textfield',
      '#parents' => $parents,
      '#title' => $this
        ->label(),
      '#required' => TRUE,
      '#attributes' => array(
        'class' => array(
          'username',
        ),
      ),
      '#default_value' => !empty($entity->name) ? $entity->name : $default,
      '#maxlength' => 255,
    );
    $form = parent::form($form, $form_state, $entity);
    return $form;
  }

  /**
   * Validate the form element.
   */
  public function formValidate($form, &$form_state, $entity, $language = LANGUAGE_NONE) {
  }

  /**
   * Submit the form element.
   */
  public function formSubmit($form, &$form_state, $entity, $language = LANGUAGE_NONE) {
    $name = $this
      ->formExtractValues($form, $form_state, $entity);
    $entity->name = $name;
  }

  /**
   * Extract the submitted values for this form element.
   */
  public function formExtractValues($form, &$form_state, $entity) {
    $parents = $form['#parents'];
    $parents[] = $this
      ->getEntityNamespace();
    $parents[] = 'name';
    $name = drupal_array_get_nested_value($form_state['values'], $parents);
    return $name;
  }

  /**
   * {@inheritdoc}
   */
  public function configureForm($form, &$form_state, $flexiform) {
    $form = parent::configureForm($form, $form_state, $flexiform);

    // Add option to turn email registration behaviour on and off for a given
    // form.
    if (module_exists('email_registration')) {
      $form['email_registration'] = array(
        '#type' => 'checkbox',
        '#title' => t('Use email registration default name behavior'),
        '#description' => t('Let email registration automatically create a username and hide this element.'),
        '#default_value' => isset($this->settings['email_registration']) ? $this->settings['email_registration'] : TRUE,
      );
    }
    $form['default_value'] = array(
      '#tree' => TRUE,
      '#type' => 'fieldset',
      '#title' => t('Default Value'),
      '#weight' => -5,
    );
    $form['default_value']['default_value'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->label() ? $this
        ->label() : t('Default'),
      '#default_value' => !empty($this->settings['default_value']['default_value']) ? $this->settings['default_value']['default_value'] : '',
      '#maxlength' => 255,
    );
    $form['default_value']['use_tokens'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use Tokens in Default Value'),
      '#default_value' => !empty($this->settings['default_value']['use_tokens']),
    );
    $form['default_value']['contexts'] = array(
      '#title' => t('Substitutions'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['default_value']['contexts']['contexts'] = $this
      ->getCtoolsSubstitutionsList();
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function configureFormSubmit($form, &$form_state, $flexiform) {
    if (module_exists('email_registration')) {
      $this->settings['email_registration'] = $form_state['values']['email_registration'];
    }
    $this->settings['default_value']['default_value'] = $form_state['values']['default_value']['default_value'];
    $this->settings['default_value']['use_tokens'] = $form_state['values']['default_value']['use_tokens'];
    parent::configureFormSubmit($form, $form_state, $flexiform);
  }

  /**
   * {@inheritdoc}
   */
  public function toSettingsArray() {
    $settings = parent::toSettingsArray();
    if (isset($this->settings['email_registration'])) {
      $settings['email_registration'] = $this->settings['email_registration'];
    }
    if (isset($this->settings['default_value'])) {
      $settings['default_value'] = $this->settings['default_value'];
    }
    return $settings;
  }

}

Classes

Namesort descending Description
FlexiformElementUserName Class to add the node title field to a form.