contest-8.x-1.0-alpha2/contest.module

contest.module
<?php

/**
 * @file
 * The contest's module file.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\contest\ContestCache;
use Drupal\contest\ContestHelper;
use Drupal\contest\ContestStorage;
use Drupal\contest\ContestUser;

/**
 * Implements hook_cache_flush().
 */
function contest_cache_flush() {
  ContestCache::flushCache();
  return [];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function contest_form_contest_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $end = !empty($form['end']['widget'][0]['value']['#default_value']) ? $form['end']['widget'][0]['value']['#default_value'] : REQUEST_TIME;
  $start = !empty($form['start']['widget'][0]['value']['#default_value']) ? $form['start']['widget'][0]['value']['#default_value'] : REQUEST_TIME;
  $usr = new ContestUser(1);

  // A link to the list page.
  $form['list'] = [
    '#type'   => 'markup',
    '#markup' => t('<a href="@url">Contest List &raquo;</a>', ['@url' => \Drupal::url('contest.contest_list')]),
    '#weight' => -1000,
  ];
  // Start date.
  $form['start_tmp'] = [
    '#title'               => t('Start'),
    '#description'         => t('Format: YYYY-MM-DD'),
    '#type'                => 'date',
    '#default_value'       => date('Y-m-d', $start),
    '#date_format'         => 'Y-m-d',
    '#date_increment'      => ContestStorage::DAY,
    '#date_label_position' => 'invisible',
    '#date_timezone'       => date('T'),
    '#date_year_range'     => '-1:+3',
    '#required'            => TRUE,
    '#weight'              => $form['start']['#weight'],
  ];
  // End date.
  $form['end_tmp'] = [
    '#title'               => t('End'),
    '#description'         => t('Format: YYYY-MM-DD'),
    '#type'                => 'date',
    '#default_value'       => date('Y-m-d', $end),
    '#date_format'         => 'Y-m-d',
    '#date_increment'      => ContestStorage::DAY,
    '#date_label_position' => 'invisible',
    '#date_timezone'       => date('T'),
    '#date_year_range'     => '-1:+3',
    '#required'            => TRUE,
    '#weight'              => $form['end']['#weight'],
  ];
  // Set a default sponsor ID.
  if (empty($form['sponsor_uid']['widget'][0]['target_id']['#default_value']->uid->value)) {
    $form['sponsor_uid']['widget'][0]['target_id']['#default_value'] = $usr->getAccount();
  }
  // Turn off some elements.
  $form['start']['widget'][0]['value']['#access'] = FALSE;
  $form['end']['widget'][0]['value']['#access'] = FALSE;

  $form['publish_winners']['widget'] = [
    '#type'  => 'hidden',
    '#value' => (isset($form['publish_winners']['#default_value']) && $form['publish_winners']['#default_value'] == 1) ? 1 : 0,
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function contest_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $birthdate = !empty($form['field_contest_birthdate']['widget'][0]['value']['#default_value']) ? $form['field_contest_birthdate']['widget'][0]['value']['#default_value'] : REQUEST_TIME;
  $states = ContestHelper::getStates(\Drupal::config('system.date')->get('country.default'));

  // Birthdate.
  if (!empty($form['field_contest_birthdate'])) {
    array_unshift($form['#validate'], 'contest_user_form_validate');

    $form['birthdate'] = [
      '#title'               => $form['field_contest_birthdate']['widget'][0]['value']['#title'],
      '#description'         => t('Format: YYYY-MM-DD'),
      '#type'                => 'date',
      '#default_value'       => date('Y-m-d', $birthdate),
      '#date_format'         => 'Y-m-d',
      '#date_increment'      => ContestStorage::DAY,
      '#date_label_position' => 'invisible',
      '#date_timezone'       => date('T'),
      '#date_year_range'     => '-99:+0',
      '#required'            => FALSE,
      '#weight'              => $form['field_contest_birthdate']['#weight'],
    ];
    $form['field_contest_birthdate']['widget'][0]['value']['#access'] = FALSE;
  }
  // Change the state text field to a select if we have the states to fill it.
  if (!empty($states) && !empty($form['field_contest_state'])) {
    $form['field_contest_state']['widget'][0]['value'] = [
      '#type'          => 'select',
      '#title'         => $form['field_contest_state']['widget'][0]['value']['#title'],
      '#options'       => array_merge(['' => '-- Select --'], $states),
      '#default_value' => !empty($form['field_contest_state']['widget'][0]['value']['#default_value']) ? $form['field_contest_state']['widget'][0]['value']['#default_value'] : '',
      '#required'      => FALSE,
      '#weight'        => $form['field_contest_state']['widget'][0]['value']['#weight'],
    ];
  }
}

/**
 * Implements hook_help().
 */
function contest_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.contest':
      $html = '<h3>' . t('About') . '</h3>';
      $html .= '<p>' . t('The contest module allows your site to host random games of chance, &ldquo;Sweepstakes&rdquo;. Where users can enter to win prizes via a random drawing.') . '</p>';
      $html .= '<p>' . t('Anonymous users can fill-in their personal information and a user will be created for them.') . '</p>';
      $html .= '<p>' . t('Logged in users with complete profiles can just click the &ldquo;Enter Contest&rdquo; button.') . '</p>';
      $html .= '<p>' . t("Winners can be selected via a contest's admin page or the entries exported and printed for a public drawing.") . '</p>';
      $html .= '<p>' . t('The same export can be provided to sponsors or affiliates for marketing purposes.') . '</p>';
      return $html;
  }
  return '';
}

/**
 * Implements hook_page_attachments().
 */
function contest_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'contest/drupal.contest-links';
}

/**
 * Implements hook_theme().
 */
function contest_theme() {
  $themes = [
    'contest_admin' => [
      'template'  => 'contest-admin',
      'variables' => [
        'contest' => NULL,
      ],
    ],
    'contest_results' => [
      'template'  => 'contest-results',
      'variables' => [
        'results' => NULL,
      ],
    ],
    'contest_tnc' => [
      'template'  => 'contest-tnc',
      'variables' => [
        'data' => NULL,
      ],
    ],
  ];
  return $themes;
}

/**
 * Implements hook_uri().
 */
function contest_uri($contest) {
  return ['path' => 'contest/' . $contest->id()];
}

/**
 * Custom validation function for the user form.
 */
function contest_user_form_validate($form, $form_state) {
  $form_state->setValue('field_contest_birthdate', [['value' => strtotime($form_state->getValue('birthdate'))]]);
}

/**
 * Preprocessor for the contest admin page.
 */
function template_preprocess_contest_admin(&$vars, $theme) {
  $vars['contest'] = ContestHelper::getContestData(\Drupal::request()->get('contest')->id());
}

/**
 * Preprocessor for the contest view page.
 */
function template_preprocess_contest_results(&$vars, $theme) {
  foreach (ContestHelper::getWinners(\Drupal::request()->get('contest')->id()) as $place => $uid) {
    $usr = new ContestUser($uid);

    $vars['results'][$place] = [
      'uid'       => $usr->uid,
      'full_name' => $usr->fullName,
      'place'     => $place,
      'city'      => $usr->city,
      'state'     => $usr->state,
      'mail'      => $usr->mail,
    ];
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc