drupal 7:在块配置中保存复选框状态

时间:2018-03-16 11:47:22

标签: php drupal-7

我创建了这个包含6个复选框的块:

<?php

/**
 * Implements hook_block_info().
 */
function menu_square_block_info() {
    $blocks['menu_square'] = array(
        'info' => t('Block Square'),
        //'cache' => DRUPAL_CACHE_PER_ROLE,
    );

    return $blocks;
}

/**
 * Implements hook_block_view().
 */
function menu_square_block_view($delta = '') {
    $block = array();
    switch ($delta) {
        case 'menu_square':
            $block['subject'] = t('block Title');
            $block['content'] = t(
                '<div id="menu-square" class="container">
                    <div class="row">
                        <div class="col-lg-4 box">
                            <a href="/search/?f[0]=type%3Abrigadista">
                                <div class="inner">
                                    <p>Brigadistas</p>
                                </div>
                            </a>
                        </div>
                        <div class="col-lg-4 box">
                            <a href="#">
                                <div class="inner">
                                    <p>Fuentes Documentales</p>
                                </div>
                            </a>
                        </div>
                        <div class="col-lg-4 box">
                            <a href="#">
                                <div class="inner">
                                    <p>Encuadramientos militares</p>
                                </div>
                            </a>
                        </div>
                    </div>
                    <div class="row">
                       <div class="col-lg-4 box">
                            <a href="#">
                                <div class="inner">
                                    <p>Proyecto Sidbrint</p>
                                </div>
                            </a>
                        </div>
                        <div class="col-lg-4 box">
                            <a href="#">
                                <div class="inner">
                                    <p>Colaboradores</p>
                                </div>
                            </a>
                        </div>
                        <div class="col-lg-4 box">
                            <a href="#">
                                <div class="inner">
                                    <p>Noticias</p>
                                </div>
                            </a>
                        </div>
                    </div>
                </div>'
            );
            break;
    }
    return $block;
}

/*
 * Implements hock_block_configure().
 **/
function menu_square_block_configure($delta='') {
    $form = array();

    switch($delta) {
    case 'menu_square':
        $boxes = array(
            'brigadistes' => t('brigadistes'),
            'fonts_documentals' => t('fonts_documentals'),
            'encuadraments_militars' => t('encuadraments_militars'),
            'projecte_sidbrint' => t('projecte_sidbrint'),
            'colaboradors' => t('colaboradors'),
            'noticies' => t('noticies')
        );    

        // Text field form element
        $form['boxes'] = array(
            '#title' => t('Capses a mostrar'),
            '#type' => 'checkboxes',
            '#description' => t('selecciona les capses a mostrar.'),
            '#options' => $boxes,
            '#default_value' => variable_get('boxes', array()),
        );
        break;
    }
    return $form;
}

/**
 * Implements hook_block_save().
 */
function menu_square_block_save($delta = '', $edit = array()) {
    switch($delta) {
    case 'menu_square' :
        // Saving the WYSIWYG text      
        variable_set('checkboxes_boxes', $edit['boxes']);
  }
}

我遇到了问题,因为当我在块配置中选中一些复选框并保存时,复选框就会被取消选中。所以我不知道如何保存价值。

如何保存所选的复选框?

1 个答案:

答案 0 :(得分:1)

请遵循此代码..

//文本字段表单元素

        $default = variable_get('checkboxes_boxes', array());
        $form['boxes'] = array(
            '#title' => t('Capses a mostrar'),
            '#type' => 'checkboxes',
            '#description' => t('selecciona les capses a mostrar.'),
            '#options' => $boxes,
            '#default_value' => $default,
        );
        break;
    }
    return $form;
}

在此处,更改 variable_get(&#39; checkboxes_boxes&#39;,array()),然后传递到$ default变量。