Skip to content
Snippets Groups Projects
ModuleConfigurationForm.php 9.62 KiB
Newer Older
<?php

namespace Drupal\wcm8_siteinfo\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
Melissa Miller's avatar
Melissa Miller committed
use Drupal\Component\Utility\UrlHelper;

/**
 * Defines a form that configures module settings.
 */
class ModuleConfigurationForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'wcm8_siteinfo_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'wcm8_siteinfo.settings',
      'system.site',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('wcm8_siteinfo.settings');
    $site_config = $this->config('system.site');
    $site_name = $site_config->get('name');
    $form['siteinfo'] = array(
      '#type' => 'vertical_tabs',
      '#default_tab' => 'edit-basic',
    );
    // Basic Information tab
    $form['basic'] = array(
      '#type' => 'details',
      '#title' => $this->t('Basic Information'),
      '#group' => 'siteinfo',
    );

    $form['basic']['site_name_prefix'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Site Name Prefix'),
      '#description' => $this->t('The prefix for your site name - for example, <i>Department of</i> ... '),
      '#size' => '60',
      '#maxlength' => '60',
      '#default_value' => $config->get('site_name_prefix'),
    $form['basic']['site_name'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Site Name'),
      '#description' => $this->t('Main name for the site - it should be your college, department or organization name.'),
      '#size' => '60',
      '#maxlength' => '75',
      '#default_value' => $site_name,
Melissa Miller's avatar
Melissa Miller committed
    $form['basic']['admin_link'] = array(
      '#type' => 'checkbox',
      '#title' => $this->t('Admin Link'),
      '#description' => $this->t('Check this box if you would like to automatically display an admin login link in your site footer. It will be displayed below the Footer Block. If you don\'t want to add the link here you can add it elsewhere by creating a link to "/user/login".'),
Melissa Miller's avatar
Melissa Miller committed
      '#default_value' => $config->get('admin_link'),
    );
    $form['basic']['footer_text'] = array(
Melissa Miller's avatar
Melissa Miller committed
      '#type' => 'text_format',
      '#title' => $this->t('Footer Text'),
Melissa Miller's avatar
Melissa Miller committed
      '#description' => $this->t('This content will be displayed in the footer below the contact info. It can contain links or other pertinent information. This is a good place for your ADA statement.'),
      '#format' => 'filtered_html',
      '#default_value' => $config->get('footer_text.value'),
    // Contact Information tab
    $form['contact'] = array(
      '#type' => 'details',
      '#title' => $this->t('Contact Information'),
Melissa Miller's avatar
Melissa Miller committed
      '#description' => $this->t('Contact information will be displayed below the Ohio State wordmark in the site footer. Leave blank any fields that you do not want to be displayed.'),
      '#group' => 'siteinfo',
    );
    $form['contact']['address_1'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Address Line 1'),
      '#description' => $this->t('First line of address. Room number and building if on campus.'),
      '#default_value' => $config->get('address_1'),
    );
    $form['contact']['address_2'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Address Line 2'),
      '#description' => $this->t('Second line of address. Street address if on campus.'),
      '#default_value' => $config->get('address_2'),
    );
    $form['contact']['city'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('City'),
      '#default_value' => $config->get('city'),
    );
    $form['contact']['state'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('State'),
      '#default_value' => $config->get('state'),
    );
    $form['contact']['zip'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Zip Code'),
      '#description' => $this->t('Formatted: 43210 or 43210-1234'),
      '#default_value' => $config->get('zip'),
    );
    $form['contact']['phone'] = array(
      '#type' => 'tel',
      '#title' => $this->t('Phone'),
      '#description' => $this->t('Formatted: 614-555-5555'),
      '#default_value' => $config->get('phone'),
    );
    $form['contact']['fax'] = array(
      '#type' => 'tel',
      '#title' => $this->t('Fax'),
      '#description' => $this->t('Formatted: 614-555-5555'),
      '#default_value' => $config->get('fax'),
    );
    $form['contact']['contact_email'] = array(
      '#type' => 'email',
      '#title' => $this->t('Email'),
      '#description' => $this->t('Main contact email for your site.'),
      '#default_value' => $config->get('contact_email'),
    // Social Networking tab
    $form['social'] = array(
      '#type' => 'details',
      '#title' => $this->t('Social Networking'),
Melissa Miller's avatar
Melissa Miller committed
      '#description' => $this->t('Linked icons will be displayed in the site footer for each social media account URL that you provide. Leave blank any fields that you do not want to be displayed.'),
      '#group' => 'siteinfo',
    );
    $form['social']['twitter'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Twitter'),
Melissa Miller's avatar
Melissa Miller committed
      '#description' => t('Enter the URL to your Twitter account.'),
      '#size' => '70',
      '#maxlength' => '150',
      '#default_value' => $config->get('twitter'),
    );
Melissa Miller's avatar
Melissa Miller committed
    $form['social']['facebook'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Facebook'),
      '#description' => t('Enter the URL to your Facebook account.'),
      '#size' => '70',
      '#maxlength' => '150',
      '#default_value' => $config->get('facebook'),
    );
Melissa Miller's avatar
Melissa Miller committed
    $form['social']['linkedin'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('LinkedIn'),
      '#description' => t('Enter the URL to your LinkedIn account.'),
      '#size' => '70',
      '#maxlength' => '150',
      '#default_value' => $config->get('linkedin'),
    );
Melissa Miller's avatar
Melissa Miller committed
    $form['social']['googleplus'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Google+'),
      '#description' => t('Enter the URL to your Google+ account.'),
      '#size' => '70',
      '#maxlength' => '150',
      '#default_value' => $config->get('googleplus'),
    );
Melissa Miller's avatar
Melissa Miller committed
    $form['social']['youtube'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('YouTube'),
      '#description' => t('Enter the URL to your YouTube account.'),
      '#size' => '70',
      '#maxlength' => '150',
      '#default_value' => $config->get('youtube'),
    );
Melissa Miller's avatar
Melissa Miller committed
    $form['social']['instagram'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Instagram'),
      '#description' => t('Enter the URL to your Instagram account.'),
      '#size' => '70',
      '#maxlength' => '150',
      '#default_value' => $config->get('instagram'),
    );
Melissa Miller's avatar
Melissa Miller committed
    $form['social']['photos'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Photos'),
      '#description' => t('Enter the URL to your photo sharing account (ex. Flickr, SmugMug, Picasa, etc...).'),
      '#size' => '70',
      '#maxlength' => '150',
      '#default_value' => $config->get('photos'),
    );

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    //set config values unique to this module
    $this->config('wcm8_siteinfo.settings')
      ->set('site_name_prefix', $values['site_name_prefix'])
Melissa Miller's avatar
Melissa Miller committed
      ->set('admin_link', $values['admin_link'])
      ->set('footer_text', $values['footer_text'])
      ->set('address_1', $values['address_1'])
      ->set('address_2', $values['address_2'])
      ->set('city', $values['city'])
      ->set('state', $values['state'])
      ->set('zip', $values['zip'])
      ->set('phone', $values['phone'])
      ->set('fax', $values['fax'])
Melissa Miller's avatar
Melissa Miller committed
      ->set('contact_email', $values['contact_email'])
      ->set('twitter', $values['twitter'])
Melissa Miller's avatar
Melissa Miller committed
      ->set('facebook', $values['facebook'])
      ->set('linkedin', $values['linkedin'])
      ->set('googleplus', $values['googleplus'])
      ->set('youtube', $values['youtube'])
      ->set('instagram', $values['instagram'])
      ->set('photos', $values['photos'])
      ->save();
      //set site name value back to system.site
      $this->configFactory->getEditable('system.site')
        ->set('name', $values['site_name'])
        ->save();
    parent::submitForm($form, $form_state);
  }


  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();

    // zip code fields
    $zipCodes = ['zip'];
    foreach ($zipCodes as $zipCode) {
      if (!empty($values[$zipCode]) && !preg_match('/^\d{5}(?:[-\s]\d{4})?$/i', $values[$zipCode])) {
        $form_state->setErrorByName($zipCode, $this->t('The Zip Code is not in the proper format.'));
      }
    }

    // phone number fields
    $phoneNumbers = ['phone', 'fax'];
    foreach ($phoneNumbers as $number) {
      if (!empty($values[$number]) && !preg_match('/^\d{3}-\d{3}-\d{4}$/i', $values[$number])) {
        $form_state->setErrorByName($number, $this->t('The ' . $number . ' number is not a valid phone number or is not in the proper format.'));
Melissa Miller's avatar
Melissa Miller committed
    // url text fields
    $urls = ['twitter', 'facebook', 'linkedin', 'googleplus', 'youtube', 'instagram', 'photos' ];
    foreach ($urls as $url) {
      $field_title = $form['social'][$url]['#title'];
      $valid_url = UrlHelper::isValid($values[$url], TRUE);
      if (!empty($values[$url]) && !$valid_url) {
        $form_state->setErrorByName($url, $this->t('The ' . $field_title . ' link is not a valid URL.'));
      }
    }