You are here

public function AddBannerForm::buildForm in Dynamic Banner 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 FormInterface::buildForm

File

src/forms/AddBannerForm.php, line 18

Class

AddBannerForm

Namespace

Drupal\dynamic_banner\forms

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $current_path = \Drupal::service('path.current')
    ->getPath();

  // Gets internal path - for eg /node/29.
  $pathArgs = explode('/', $current_path);
  $dbid = isset($pathArgs[5]) ? $pathArgs[5] : '';

  // the last portion of the url there must be a better way of doing this
  // This is used by the file handler, It is needed to accept files
  $form['#attributes'] = array(
    'enctype' => "multipart/form-data",
  );
  $file_path = drupal_get_path('module', 'file');

  // default the variables here
  $banner = NULL;

  // prevent bugs nulify the variable
  $default_flag = FALSE;

  // enable variable in this scope
  // hide it so that the user may not change this element
  $form['dbid'] = array(
    '#type' => 'hidden',
    '#required' => FALSE,
  );
  if ($dbid == '' || $dbid == '0') {

    // this will disable the path field for the default banner
    if (isset($_GET['q']) && strrpos($_GET['q'], "/default")) {
      $form['#title'] = $this
        ->t('Default Banner');

      // load the default if there is one
      $banner = $this
        ->dynamic_banner_find_load_default();
    }
    else {
      $form['#title'] = $this
        ->t('New Banner');
      $form['dbid']['#default_value'] = NULL;
    }
  }
  else {

    // The dbid is set so a banner must exist load it
    $banner = $this
      ->dynamic_banner_load_banner($dbid);
    $form['#title'] = $this
      ->t('Edit Banner');

    //print_r($banner);

    // exit;

    //drupal_set_title(t("Edit Banner") . " '" . $banner->path . "'");
    $form['dbid']['#default_value'] = $dbid;
  }

  // this will prevent the used from changing this field once the default has been loaded
  // it deals with a bug if the person chose to edit the specific banner for default rather than pressing default
  if ($banner && $banner->path == 'DEFAULT') {
    $default_flag = TRUE;
  }

  // disable the path form element when the default flag is out
  if (!$default_flag) {
    $form['path'] = array(
      '#type' => 'textfield',
      '#title' => t('Banner Path'),
      '#default_value' => $banner ? $banner->path : "",
      '#size' => 45,
      '#maxlength' => 250,
      '#description' => t('Specify an existing url path you wish to put a banner on. For example: home, user* (wild card), content! (random). Enter a path as it appears in the url of your site.'),
      '#field_prefix' => Url::fromRoute('<front>', [], [
        'absolute' => TRUE,
      ]),
      '#required' => TRUE,
    );
  }
  else {
    $form['path'] = array(
      '#type' => 'hidden',
      '#title' => t('Banner Path'),
      '#default_value' => 'DEFAULT',
    );
  }

  /*// if the module exists add the autocomplete path
    // i might have to do my own autocomplete here cause mpac doesnt really do what i need it to do
    if ( module_exists('mpac') ) {
      $form['path']['#autocomplete_path'] = 'mpac/autocomplete/alias';
    }*/
  $img_arr = array(
    t('Use Existing Image(s)'),
    t('Upload New Image(s)'),
  );
  $form['image_type'] = array(
    '#type' => 'radios',
    '#options' => array_combine($img_arr, $img_arr),
    '#title' => t('Choose image type.'),
  );
  if ($banner && isset($banner->imgurl)) {
    $form['image_type']['#default_value'] = t('Use Existing Image(s)');
  }
  elseif ($banner && isset($banner->imgfid)) {
    $form['image_type']['#default_value'] = t('Upload New Image(s)');
  }

  /**
   * Note: There are two form elements for the same thing
   * They are both not required but only one is needed for proper handling
   * When we are loading an old banner load the url into imgurl
   * When we are uploading a new image the validator will upload the image store it and fill in imgurl for you
   * Only use one method no mix and matching
   * When reading the data use checks to see which method was used
   */
  global $base_url;
  $form['imgurl'] = array(
    '#type' => 'textfield',
    '#title' => t('Typeout the url of the image'),
    '#default_value' => $banner ? $banner->imgurl : '',
    '#description' => t('Specify an image(s) for the banner to display.'),
    '#field_prefix' => $base_url . '/sites/default/files',
    '#states' => array(
      'visible' => array(
        ':input[name="image_type"]' => array(
          'value' => 'Use Existing Image(s)',
        ),
      ),
    ),
  );
  $form['image'] = array(
    '#title' => t('Choose Image File'),
    '#type' => 'managed_file',
    '#default_value' => $banner ? array(
      $banner->imgfid,
    ) : 0,
    '#progress_indicator' => 'throbber',
    '#progress_message' => NULL,
    '#upload_location' => \Drupal::config('dynamic_banner.settings')
      ->get('dynamic_banner_file_save_path', BANNER_DEFAULT_SAVE_LOCATION),
    '#description' => t('Specify an image(s) for the banner to display.'),
    '#upload_validators' => [
      'file_validate_extensions' => array(
        'jpg jpeg png gif',
      ),
    ],
    '#states' => array(
      'visible' => array(
        ':input[name="image_type"]' => array(
          'value' => 'Upload New Image(s)',
        ),
      ),
    ),
  );

  /**
   * Since upon pressing the delete button on the image the fid is set to 0
   * We need to save is because we still need to delete that image.
   */
  $form['oldimagefid'] = array(
    '#type' => 'hidden',
    '#required' => FALSE,
    '#default_value' => $banner ? $banner->imgfid : 0,
  );
  $form['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Text'),
    '#default_value' => $banner ? $banner->text : '',
    '#maxlength' => 250,
    '#size' => 45,
    '#description' => t('Specify the text to associate with this banner [comma seperated for randoms, also must match amount of elements from images] (optional).'),
    '#required' => FALSE,
  );
  $form['link'] = array(
    '#type' => 'textfield',
    '#title' => t('Link'),
    '#default_value' => $banner ? $banner->link : '',
    '#maxlength' => 250,
    '#size' => 45,
    '#description' => t('Specify the link you want your banner to point to (optional).'),
    '#required' => FALSE,
  );
  $mode_arr = array(
    t('normal'),
    t('time_based'),
    t('rotating'),
    t('fade'),
  );
  $form['mode'] = array(
    '#type' => 'radios',
    '#title' => t('Mode'),
    '#options' => array_combine($mode_arr, $mode_arr),
    '#default_value' => $banner ? $banner->mode : BANNER_DEFAULT_BANNER_MODE,
    '#description' => t('What mode do you want this banner to display under (this is different than display setting)'),
    '#required' => TRUE,
  );

  /*
    $form['time_on'] = array(
      '#type'          => 'date',
      '#title'         => t('Start Time'),
      '#description'   => t('Specify the time you want your banner to start displaying (optional).'),
      '#required'      => FALSE,
      '#states'        => array(
        'visible'      => array(
          ':input[name="mode"]' => array('value' => t('time_based')),
        ),
      ),
    );

    $form['time_off'] = array(
      '#type'          => 'date',
      '#title'         => t('End Time'),
      '#description'   => t('Specify the time you want your banner to stop displaying (optional).'),
      '#required'      => FALSE,
      '#states'        => array(
        'visible'      => array(
          ':input[name="mode"]' => array('value' => t('time_based')),
        ),
      ),
    );*/
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Banner'),
  );
  $form['#attached']['library'][] = 'dynamic_banner/bannerattach';
  return $form;
}