5 * This module allows the site admin to set advanced front page settings.
7 * This version is for Drupal 8.
8 * Earlier versions can be found at http://drupal.org/project/front.
10 * This module version was developed by timhilliard and various members
11 * of the drupal community.
13 * If you have any ideas/patches or requests,
14 * please post them at http://drupal.org/project/issues/front.
17 use Drupal\Core\Database\Database;
18 use Drupal\Core\Render\Element;
19 use \Drupal\Core\Link;
23 * Implements hook_help().
25 function front_page_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
26 switch ($route_name) {
27 case 'front_page.settings':
28 return t('<p>Setup custom front pages for your site.</p>');
30 case 'front_page.home_links':
31 return t('<p>If a HOME link is set, the <front> placeholder will be replaced with this value instead of the standard front page.</p>');
36 * Function to parse a full URL including GET variables and fragment
37 * to an array ready for drupal_goto(), url(), or l() functions.
39 function front_page_parse_url($path) {
41 $url['options'] = array();
42 if (preg_match('@^(?P<path>[^?#]+)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$@', $path, $match)) {
43 $url['path'] = $match['path'];
44 if (!empty($match['query'])) {
45 foreach (explode('&', $match['query']) as $query_part) {
46 list($key, $value) = explode('=', $query_part);
47 $url['options']['query'][$key] = $value;
50 if (!empty($match['fragment'])) {
51 $url['options']['fragment'] = $match['fragment'];
58 * Function to return the first role enabled in front page, ordered by weight.
60 function front_page_get_by_role($index = 0, $number = 1) {
61 $roles = \Drupal::currentUser()->getRoles();
62 $result = Database::getConnection()->select('front_page', 'fp')
64 ->condition('rid', $roles, 'IN')
65 ->condition('mode', '', '<>')
66 ->orderBy('weight', 'ASC')
67 ->orderBy('rid', 'DESC')
68 ->range($index, $number)
75 * Function to return the first role enabled in front page, ordered by weight.
77 function front_page_get_by_rid($rid) {
79 $result = Database::getConnection()->select('front_page', 'fp')
81 ->condition('rid', $rid)
82 ->condition('mode', '', '<>')
89 * Function to return all the roles in front page, ordered by weight.
91 function front_page_get_all() {
92 $result = Database::getConnection()->select('front_page', 'fp')
94 ->orderBy('weight', 'ASC')
95 ->orderBy('rid', 'DESC')
97 ->fetchAllAssoc('rid', PDO::FETCH_ASSOC);
102 * Implements hook_theme().
104 function front_page_theme() {
106 'front_page_admin_arrange_form' => array(
107 'render element' => 'form',
108 'function' => 'theme_front_page_admin_arrange_form',
114 * Implements hook_user_role_delete().
116 function front_page_user_role_delete($role) {
117 // Delete Front configuration for the role being deleted.
118 Database::getConnection()->delete('front_page')
119 ->condition('rid', $role->rid)
124 * Returns HTML for the front page arrange form into a table.
126 * @param array $variables
127 * An associative array containing:
128 * - form: A render element representing the form.
130 function theme_front_page_admin_arrange_form($variables) {
131 $form = $variables['form'];
133 // Enable the drag handles.
134 drupal_attach_tabledrag($form['roles'], array(
135 'table_id' => 'front-page-arrange',
137 'relationship' => 'sibling',
138 'group' => 'front-page-weight',
150 $renderer = \Drupal::service('renderer');
151 foreach (Element::children($form['roles']) as $rid) {
152 $element = &$form['roles'][$rid];
154 // Add special classes to be used for tabledrag.js.
155 $element['weight']['#attributes']['class'] = array('front-page-weight');
158 $row[] = $renderer->render($element['title'], FALSE);
159 $row[] = $renderer->render($element['mode'], FALSE);
160 $row[] = $renderer->render($element['preview'], FALSE);
161 $row[] = $renderer->render($element['enabled'], FALSE);
162 $row[] = $renderer->render($element['weight'], FALSE);
164 $row = array_merge(array('data' => $row), $element['#attributes']);
165 $row['class'][] = 'draggable';
170 $rows[] = array(array('data' => 'no roles', 'colspan' => '5'));
173 $front_page_arrange = [
175 '#header' => $header,
177 'attributes' => array('id' => 'front-page-arrange'),
179 $output .= $renderer->render($front_page_arrange);
180 $output .= drupal_render_children($form);