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 * If you have any ideas/patches or requests,
11 * please post them at http://drupal.org/project/issues/front.
14 use Drupal\Core\Database\Database;
15 use Drupal\Core\Render\Element;
16 use Drupal\Core\Routing\RouteMatchInterface;
19 * Implements hook_help().
21 function front_page_help($route_name, RouteMatchInterface $route_match) {
22 switch ($route_name) {
23 case 'front_page.settings':
24 return t('<p>Setup custom front pages for your site.</p>');
26 case 'help.page.front_page':
27 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>');
32 * Parse URL including GET and fragment.
34 * Function to parse a full URL including GET variables and fragment
35 * to an array ready for drupal_goto(), url(), or l() functions.
37 function front_page_parse_url($path) {
40 if (preg_match('@^(?P<path>[^?#]+)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$@', $path, $match)) {
41 $url['path'] = $match['path'];
42 if (!empty($match['query'])) {
43 foreach (explode('&', $match['query']) as $query_part) {
44 list($key, $value) = explode('=', $query_part);
45 $url['options']['query'][$key] = $value;
48 if (!empty($match['fragment'])) {
49 $url['options']['fragment'] = $match['fragment'];
56 * Function to return the first role enabled in front page, ordered by weight.
58 function front_page_get_by_role($index = 0, $number = 1) {
59 $roles = \Drupal::currentUser()->getRoles();
60 $result = Database::getConnection()->select('front_page', 'fp')
62 ->condition('rid', $roles, 'IN')
63 ->condition('mode', '', '<>')
64 ->orderBy('weight', 'ASC')
65 ->orderBy('rid', 'DESC')
66 ->range($index, $number)
73 * Function to return the first role enabled in front page, ordered by weight.
75 function front_page_get_by_rid($rid) {
77 $result = Database::getConnection()->select('front_page', 'fp')
79 ->condition('rid', $rid)
80 ->condition('mode', '', '<>')
87 * Function to return all the roles in front page, ordered by weight.
89 function front_page_get_all() {
90 $result = Database::getConnection()->select('front_page', 'fp')
92 ->orderBy('weight', 'ASC')
93 ->orderBy('rid', 'DESC')
95 ->fetchAllAssoc('rid', PDO::FETCH_ASSOC);
100 * Implements hook_theme().
102 function front_page_theme() {
104 'front_page_admin_arrange_form' => [
105 'render element' => 'form',
106 'function' => 'theme_front_page_admin_arrange_form',
112 * Implements hook_user_role_delete().
114 function front_page_user_role_delete($role) {
115 // Delete Front configuration for the role being deleted.
116 Database::getConnection()->delete('front_page')
117 ->condition('rid', $role->rid)
122 * Returns HTML for the front page arrange form into a table.
124 * @param array $variables
125 * An associative array containing:
126 * - form: A render element representing the form.
129 * Rendered admin form.
131 * @todo Refactor this function, it's a legacy way.
133 function theme_front_page_admin_arrange_form($variables) {
134 $form = $variables['form'];
136 // Enable the drag handles.
137 drupal_attach_tabledrag($form['roles'], [
138 'table_id' => 'front-page-arrange',
140 'relationship' => 'sibling',
141 'group' => 'front-page-weight',
153 $renderer = \Drupal::service('renderer');
154 foreach (Element::children($form['roles']) as $rid) {
155 $element = &$form['roles'][$rid];
157 // Add special classes to be used for tabledrag.js.
158 $element['weight']['#attributes']['class'] = ['front-page-weight'];
161 $row[] = $renderer->render($element['title'], FALSE);
162 $row[] = $renderer->render($element['mode'], FALSE);
163 $row[] = $renderer->render($element['preview'], FALSE);
164 $row[] = $renderer->render($element['enabled'], FALSE);
165 $row[] = $renderer->render($element['weight'], FALSE);
167 $row = array_merge(['data' => $row], $element['#attributes']);
168 $row['class'][] = 'draggable';
173 $rows[] = [['data' => 'no roles', 'colspan' => '5']];
176 $front_page_arrange = [
178 '#header' => $header,
180 'attributes' => ['id' => 'front-page-arrange'],
182 $output .= $renderer->render($front_page_arrange);
183 $output .= drupal_render_children($form);