Set values in multiple select list using javascript and jQuery

We can make multiple option selected in our HTML form using JavaScript and jQuery.

In Javascript:

document.getElementById('elementId').value = ["optionVal1", "optionVal2", "optionVal3"];

and in Jquery Its pretty:

$("#elementId").val(["optionVal1", "optionVal2", "optionVal3"]);
Advertisements

How to add HTML in drupal form

If you want to add HTML in Drupal form using form api. you need to use #markup in that form field.

See the below example of form field:


 $form['form_field'] = array(
 
  '#markup' => 'HTML code here', // and add HTML in #markup. 
 
);

get drupal taxonomy term list using vocabulary

Get drupal 7 taxonomy term list using vocabulary machine name

function get_taxonomy_terms_by_vocabulary($vocabulary_machine_name){

$term_data = array();
$vid = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name)->vid;
$terms = taxonomy_get_tree($vid);

//If you want to get more custom fields value, than use
//$terms = entity_load('taxonomy_term', FALSE, array('vid' => $vid));

foreach($terms as $term){
$term_data[$term->tid] = $term->name;
}

return $term_data;
}

Submit drupal form & node_save using hook_submit_form in Drupal 7

function user_identity_form_submit($form, &$form_state) {
  global $user;
  global $base_url;
  //variable_set('identity_image', $form_state['values']['identity_image']);
  $file = file_load($form_state['values']['identity_image']);
  if($file){
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    file_usage_add($file, 'user_identity', 'user_identity', $file->uid);

    //create node identity
    $node = new stdClass();
    $node->type = 'user_identity';
    node_object_prepare($node);
    $node->title = 'Identity: '.$user->name;
    $node->field_photo_document['und'][] = (array)$file;
    $node->language = LANGUAGE_NONE;
    $node->uid = $user->uid;
    $node->status = 0;
    node_save($node);
...

Display image after upload using managed_file in Drupal 7

function MODULE_NAME_theme() {
    return array(
      'MODULE_NAME_image' => array(
        'render element' => 'form',
      ),
    );
}

function theme_MODULE_NAME_image($variables) {
    $form = $variables['form'];
    if(isset($form['#file']->uri))
        $form['identity_image']['#markup'] = theme('image_style', array('style_name' => 'thumbnail', 'path' => file_build_uri(file_uri_target($form['#file']->uri))));
    $output = drupal_render_children($form);
    return $output;
}

function MODULE_NAME_form($form, &$form_state) {
    $form = array();
        $form = array();
        $form['identity_image'] = array(
          '#title' => t('Upload photo document'),
          '#type' => 'managed_file',
          '#required' => true,
          '#upload_location' => 'public://identity/'.$order->uid.'/',
          '#upload_validators' => array(
            'file_validate_extensions' => array('jpg png jpeg'),
          ),
          '#theme' => 'MODULE_NAME_image',
        );
        $form['submit'] = array(
          '#type' => 'submit',
          '#value' => t('Approved'),
        );
return $form;
}

Drupal Custom php file

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Now you can use drupal database with drupal's dbal:
// Unlock user admin if blocked
db_query("UPDATE {users} set status = 1 where uid = 1");

$nid = db_insert('table')
->fields(array(
  'numbers' => ('23456')
  ))
->execute();
drupal_set_message(t('Content was added'));

Fields setting stored in drupal 7

Base field data is in field_config, and instance data is in field_config_instance. Rather than modifying them manually, though, you can always use the API:

$field = field_info_field('field_name');
$field['key'] = $new_value;
field_update_field($field);

Or for an instance

$instance = field_info_instance('node', 'field_name', 'article');
$instance['key'] = $new_value;
field_update_instance($instance);

Using the API will clear the appropriate caches, and it’s just a better idea than editing the data manually.

Either way, make sure to backup the database first in case something goes horribly wrong.