php /** * @file * Code for the RAC News feature. */ define('RAC_NEWS_SCREEN_LIMIT', 20); include_once 'rac_news.features.inc'; include_once 'rac_news.class.inc'; include_once 'rac_news.cron.inc'; /** * Implements hook_permission(). */ function rac_news_permission() : array { return [ 'administer notification email' => [ 'title' => t('Administer News Article notification'), ], 'read news articles' => [ 'title' => t('Who can read News Article.'), ], ]; } /** * Implements hook_menu(). */ function rac_news_menu() : array { $items = []; $items['news'] = [ 'title' => 'News', 'description' => 'List all news articles.', 'page callback' => 'rac_news_page', 'access arguments' => ['read news articles'], 'file' => 'rac_news.page.inc', 'type' => MENU_LOCAL_TASK, 'weight' => 10, ]; $items['admin/people/notification'] = [ 'title' => 'News Notification', 'description' => 'Configure the News Article notification email.', 'page callback' => 'drupal_get_form', 'page arguments' => ['rac_news_notification_config'], 'access arguments' => ['administer notification email'], 'type' => MENU_LOCAL_TASK, 'weight' => 10, ]; return $items; } /** * Implements hook_theme(). */ function rac_news_theme($existing, $type, $theme, $path) : array { return [ 'rac_news_list' => [ 'variables' => [ 'items' => [], ], 'file' => 'rac_news.theme.inc', ], ]; } /** * Loads all news articles. * * Forces a limit of RAC_NEWS_SCREEN_LIMIT rows. * * @param string $contract * Contract type either v1 or v2. * * @return array * Rac News array of objects. */ function rac_news_list_load(string $contract = '') : array { // Build query. $query = db_select('node', 'n'); $query->leftJoin('field_data_field_news_contract_type', 'ct', 'n.nid = ct.entity_id'); $query->leftJoin('field_data_body', 'b', 'n.nid = b.entity_id'); $query->leftJoin('field_data_field_image', 'img', 'n.nid = img.entity_id'); $query->fields('n', ['nid', 'title']); $query->addField('img', 'field_image_fid', 'image'); $query->addField('img', 'field_image_width', 'image_width'); $query->addField('img', 'field_image_height', 'image_height'); $query->addField('b', 'body_value', 'body'); $query->condition('n.type', 'rac_news_article'); $query->condition('n.status', NODE_PUBLISHED); // Query condition depending on contract type. if (!empty($contract)) { $contracts = ['all']; if ($contract === 'v1' || $contract === 'v2') { $contracts[] = $contract; } $query->condition('ct.field_news_contract_type_value', $contracts, 'IN'); } $query->orderBy('n.created', 'DESC'); // Limit the query to RAC_NEWS_SCREEN_LIMIT rows. $query = $query->extend('PagerDefault')->limit(RAC_NEWS_SCREEN_LIMIT); // Execute the query. $results = $query->execute(); return $results->fetchAll(); } function rac_news_notification_config($form, $form_state) { $text = variable_get('rac_news_notificationtext', 'New news content!'); $form['notificationtext'] = array( '#title' => t('Notification Text'), '#type' => 'textarea', '#description' => t('The email that will be sent to the user.'), '#default_value' => $text, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); return $form; } function rac_news_notification_config_submit($form_id, &$form_state) { drupal_set_message('Your settings have been saved'); variable_set('rac_news_notificationtext', $form_state['values']['notificationtext']); } function rac_news_mail($key, &$message, $params) { if ($key == 'news_notification') { $message['subject'] = t('New news item on @site_name', array('@site_name' => variable_get('site_name', 'The site'))); $message['body'][] = t('Hello @user,', array('@user' => format_username($params['account']))); $message['body'][] = ''; $message['body'][] = t(variable_get('rac_news_notificationtext', 'New news content!')); $message['body'][] = '
' . drupal_render($params['image']) . ' | ' . t($params['title']) . ' ' . l('View Article', $params['link'], array('theme' => 'email')); $message['body'][] = ' |