0

This question already has an answer here:

I've created a settings page for my theme, and I'd like to be able to set the frontpage from there, but I don't know how I can change the frontpage in code?

This is the setting:

$node_type = "page";
$result = db_query(
    'SELECT n.nid, n.title FROM {node} n WHERE n.type = :type',
    array(':type' => $node_type)
);
$options = array();
foreach ($result as $record) {
    $options[$record->nid] = $record->title;
}

$form['static_nodes']['front_page_id'] = array(
    '#type'        => 'select',
    '#title'       => t('Frontpage'),
    '#options'     => $options,
    '#description' => t('Set the page used as the frontpage.'),
    '#default_value' => theme_get_setting('front_page_id'),
);

marked as duplicate by Mołot, Triskelion, monymirza, Chaulky, AjitS Aug 23 '13 at 5:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Theme is not supposed to touch logic. It is supposed to be about presentation only. Thus it might be possible, but should be avoided. – Mołot Aug 22 '13 at 10:57
  • @Mołot : This is my first project in Drupal, I've learned a lot I need to do differently next time, but for now I just need to get it working xD variable_set('site_frontpage', $value), but where should I set that then? could I hook into the "save settings" button in some way, and set the variable there? – martindilling Aug 22 '13 at 11:41
  • Yes – Mołot Aug 22 '13 at 11:44
  • Thanks a lot ^^ added my solution to the original post if anyone should be interested :) – martindilling Aug 22 '13 at 12:13
4

Solution:

function billetsalget_form_system_theme_settings_alter(&$form, &$form_state)
{
    /**
     * The group fieldset
     */
    $form['static_nodes'] = array(
        '#type'          => 'fieldset',
        '#title'         => t('Static Pages'),
        '#weight'        => -2,
    );

    /**
     * Get pages from db as options
     */
    $node_type = "page";
    $result = db_query(
        'SELECT n.nid, n.title FROM {node} n WHERE n.type = :type',
        array(':type' => $node_type)
    );
    $options = array();
    foreach ($result as $record) {
        $options[$record->nid] = $record->title;
    }

    /**
     * Select dropdown for Frontpage
     */
    $form['static_nodes']['front_page_id'] = array(
        '#type'        => 'select',
        '#title'       => t('Frontpage'),
        '#options'     => $options,
        '#description' => t('Set the page used as the frontpage.'),
        '#default_value' => theme_get_setting('front_page_id'),
    );

    /**
     * Run function when form is submittet
     */
    $form['#submit'][] = 'billetsalget_form_system_theme_settings_submit';
}

function billetsalget_form_system_theme_settings_submit(&$form, &$form_state)
{
    /**
     * Set frontpage to 'node/' + node_id selected in the form
     */
    variable_set('site_frontpage', 'node/' . $form_state['values']['front_page_id']);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.