form_alter e codice attivabile

3 contenuti / 0 new
Ultimo contenuto
form_alter e codice attivabile

function modulo_form_alter(&$form, $form_state, $form_id)
{
inserire un check o radio che se attivato dall'user
esegue il codice :
unset[..];
}

quindi nella stessa form inserire opzione che quando attivata esegue il codice successivo;
di norma quando la form è caricata viene eseguuito l'unset;
volevo provare a gestire invece dalla form stessa;
credo serva anche un reload della pagina oppure qualche soluzione con js;

Ti riporto il form alter di comment.module può essere interessante analizzarlo:

/**
* Implementation of hook_form_alter().
*/
function comment_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    $form['comment'] = array(
      '#type' => 'fieldset',
      '#title' => t('Comment settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['comment']['comment'] = array(
      '#type' => 'radios',
      '#title' => t('Default comment setting'),
      '#default_value' => variable_get('comment_'. $form['#node_type']->type, COMMENT_NODE_READ_WRITE),
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
    );
    $form['comment']['comment_default_mode'] = array(
      '#type' => 'radios',
      '#title' => t('Default display mode'),
      '#default_value' => variable_get('comment_default_mode_'. $form['#node_type']->type, COMMENT_MODE_THREADED_EXPANDED),
      '#options' => _comment_get_modes(),
      '#description' => t('The default view for comments. Expanded views display the body of the comment. Threaded views keep replies together.'),
    );
    $form['comment']['comment_default_order'] = array(
      '#type' => 'radios',
      '#title' => t('Default display order'),
      '#default_value' => variable_get('comment_default_order_'. $form['#node_type']->type, COMMENT_ORDER_NEWEST_FIRST),
      '#options' => _comment_get_orders(),
      '#description' => t('The default sorting for new users and anonymous users while viewing comments. These users may change their view using the comment control panel. For registered users, this change is remembered as a persistent user preference.'),
    );
    $form['comment']['comment_default_per_page'] = array(
      '#type' => 'select',
      '#title' => t('Default comments per page'),
      '#default_value' => variable_get('comment_default_per_page_'. $form['#node_type']->type, 50),
      '#options' => _comment_per_page(),
      '#description' => t('Default number of comments for each page: more comments are distributed in several pages.'),
    );
    $form['comment']['comment_controls'] = array(
      '#type' => 'radios',
      '#title' => t('Comment controls'),
      '#default_value' => variable_get('comment_controls_'. $form['#node_type']->type, COMMENT_CONTROLS_HIDDEN),
      '#options' => array(
        t('Display above the comments'),
        t('Display below the comments'),
        t('Display above and below the comments'),
        t('Do not display')),
      '#description' => t('Position of the comment controls box. The comment controls let the user change the default display mode and display order of comments.'),
    );
    $form['comment']['comment_anonymous'] = array(
      '#type' => 'radios',
      '#title' => t('Anonymous commenting'),
      '#default_value' => variable_get('comment_anonymous_'. $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT),
      '#options' => array(
        COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
        COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
        COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information')),
      '#description' => t('This option is enabled when anonymous users have permission to post comments on the <a href="@url">permissions page</a>.', array('@url' => url('admin/user/permissions', array('fragment' => 'module-comment')))),
    );
    if (!user_access('post comments', drupal_anonymous_user())) {
      $form['comment']['comment_anonymous']['#disabled'] = TRUE;
    }
    $form['comment']['comment_subject_field'] = array(
      '#type' => 'radios',
      '#title' => t('Comment subject field'),
      '#default_value' => variable_get('comment_subject_field_'. $form['#node_type']->type, 1),
      '#options' => array(t('Disabled'), t('Enabled')),
      '#description' => t('Can users provide a unique subject for their comments?'),
    );
    $form['comment']['comment_preview'] = array(
      '#type' => 'radios',
      '#title' => t('Preview comment'),
      '#default_value' => variable_get('comment_preview_'. $form['#node_type']->type, COMMENT_PREVIEW_REQUIRED),
      '#options' => array(t('Optional'), t('Required')),
      '#description' => t("Forces a user to look at their comment by clicking on a 'Preview' button before they can actually add the comment"),
    );
    $form['comment']['comment_form_location'] = array(
      '#type' => 'radios',
      '#title' => t('Location of comment submission form'),
      '#default_value' => variable_get('comment_form_location_'. $form['#node_type']->type, COMMENT_FORM_SEPARATE_PAGE),
      '#options' => array(t('Display on separate page'), t('Display below post or comments')),
    );
  }
  elseif (isset($form['type']) && isset($form['#node'])) {
    if ($form['type']['#value'] .'_node_form' == $form_id) {
      $node = $form['#node'];
      $form['comment_settings'] = array(
        '#type' => 'fieldset',
        '#access' => user_access('administer comments'),
        '#title' => t('Comment settings'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => 30,
      );
      $form['comment_settings']['comment'] = array(
        '#type' => 'radios',
        '#parents' => array('comment'),
        '#default_value' => $node->comment,
        '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
      );
    }
  }
}

è uno dei tanti moduli che fanno uso di hook_form_alter per modificare varie form;

il mio caso è diverso:
per attinenza con il codice postato

if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    $form['comment'] = array(
      '#type' => 'fieldset',
      ...
    );

dovrebbee essere modificato in:

if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {

qui un check che se user abilita allora crea il fieldset sotto
     $form['comment'] = array(
      '#type' => 'fieldset',
      ...
    );

nel tuo esempio l'elemento è creato insieme alla pagina;
nel mio no: all'inizio è creata solo la pagina con un'opzione (check);

solo quando l'user abilita il check l'elemento dovrà essere caricato;