0

I added a field select list comments in a hook_form_alter. When you select and save the comment, I do not know how to preserve the value of this field when editing this comment. My Code

function comment_author_form_comment_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'comment_node_zayavka_form') {
    $query = db_select('node', 'n')
      ->fields('n', array('type', 'uid', 'title', 'nid'))
      ->condition('n.type', 'brigada_type', '=');
    $result = $query->execute();
    $brigade_list = array();
    foreach ($result as $value) {
      if ($user->uid != 1) {
        if ($value->uid == $user->uid) {
          $brigade_list[$value->nid] = $value->title;
        }
      }
      else {
        $brigade_list[$value->nid] = $value->title;
      }
    }
    $form['select_brigade'] = array(
      '#type' => 'select',
      '#title' => t('Select brigade'),
      '#options' => $brigade_list,
      '#description' => t('Please select the team that was sent to you with reference to this application.'),
      '#required' => TRUE,
      '#default_value' => variable_get('comment_field_brigade_selection', 0),
    );
  }
}
3
  • You set variable_get('comment_field_brigade_selection') as default value, but are you saving the value to this variable on submitting the form? Oct 12 '13 at 8:03
  • I redefined for this form of function comment_form_submit Oct 12 '13 at 9:28
  • And I think maybe I need to create a list box with field_create_field? Oct 12 '13 at 9:30
0

I finally solved my problem, has created a field using field_create_field and attached it to the essence of my comment present the type of material My code for the solutions in the Custom Module:

/**
 * Реализация hook_enable().
 */
function comment_author_enable() {
    foreach (_comment_author_select_field_create() as $field) {
        field_create_field($field);
    }
    foreach (_comment_author_select_field_instances() as $instance) {
        $instance['entity_type'] = 'comment';
        $instance['bundle'] = 'comment_node_zayavka';
        field_create_instance($instance);
    }
}
function _comment_author_select_field_create(){
  $fields['brigade_select'] = array(
    'field_name' => 'brigade_select',
    'label' => t('Select an option from the list'),
    'type' => 'list_text',
    'cardinality' => '1', // change this to -1 to enable ctrl+select
    'foreign keys' => array(),
    'indexes' => array(
      'value' => array(
        0 => 'value',
      ),
    ),
    'module' => 'list',
    'settings' => array(
      'allowed_values_function' => '_comment_author_options_list',
    ),
  );

  return $fields;
}
function _comment_author_select_field_instances()  {
    $instances['brigade_select'] = array(
    'field_name' => 'brigade_select',
    'label' => t('Select brigade'),
    'description' => t('Please select the team that was sent to you with reference to this application.'),
    'default_value' => NULL,    // add a default value here that matches your key => index values
    'display' => array(
      'default' => array(
        'label' => 'above',
        'module' => 'list',
        'settings' => array(),
        'type' => 'list_default',
        'weight' => -1,
      ),
      'teaser' => array(
        'label' => 'above',
        'settings' => array(),
        'type' => 'hidden',
        'weight' => -1,
      ),
    ),
    'required' => 1,
    'settings' => array(
      'user_register_form' => FALSE,
    ),
    'widget' => array(
      'active' => 1,
      'module' => 'options',
      'settings' => array(),
      'type' => 'options_select',
      'weight' => '-1',
    ),
  );
  return $instances;
}
function _comment_author_options_list(){
global $user;
$query = db_select('node', 'n')
    ->fields('n', array('type', 'uid', 'title', 'nid'))
    ->condition('n.type', 'brigada_type', '=');
 $result = $query->execute();
 $brigade_list = array();
    foreach($result as $value){
        if($user->uid != 1){
            if($value->uid == $user->uid){
             $brigade_list[$value->nid] = $value->title;
            }   
        }else{
        $brigade_list[$value->nid] = $value->title;
        }
    }
    return $brigade_list;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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