I have created a custom element in D7. The fields I have chosen are a select list for month & a text field for the year. But this render in two line while I want this in a line like date format. Please help me. I am posting the code below.\

<?php

// hook_element_info()

function custom_work_element_info() {
$elements['bucket'] = array(
'#input' => TRUE,
'#default_value' => '',
'#tree' => TRUE,
'#process' => array('custom_work_bucket_element_process'),
'#theme' => array('custom_work_theme_field'),
'#theme_wrappers' => array('form_element'),
'#value_callback' => 'custom_work_bucket_element_value_callback',
'#element_validate' => array('custom_work_bucket_element_validate'),
);
return $elements;
}

// process callback function to implement process of element

function custom_work_bucket_element_process($element, $form_state, $complete_form ) {
$element['bucket']['select_list'] = array(
'#type' => 'select',
'#empty_option' => '- ' . t('Month') . ' -',
'#options' => array('month1' => 'Jan', 'month2' => 'Feb','month3' => 'Mar','month4' => 'Apr',
'month5' => 'May','month6' => 'Jun','month7' => 'July','month8' => 'Aug',
'month9' => 'Sept','month10' => 'Oct','month11' => 'Nov','month12' => 'Dec', ),
'#required' => TRUE,
'#title' => t('Select List Title'),
'#title_display' => 'invisible',
);
if (isset($element['#default_value']['bucket']['select_list'])) {
$element['bucket']['select_list']['#default_value'] = $element['#default_value']['bucket']['select_list'];
}
$element['bucket']['textbox_fill'] = array(
'#type' => 'textfield',
'#size' => 10,
'#required' => $element['#required'],
'#title' => t('Textfield Option'),
'#title_display' => 'invisible',
'#theme_wrappers' => array(),
'#maxlength' => 4,
'#attributes' => array('placeholder' => 'year'),
);

if (isset($element['#default_value']['bucket']['textbox_fill'])) {
$element['bucket']['textbox_fill']['#default_value'] = $element['#default_value']['bucket']['textbox_fill'];
}

return $element;
}

// now the theme function for the element using hook_theme().

function custom_work_theme() {
return array(
'custom_work_theme_field' => array(
'render element' => 'element',
),
);
}

// theme function implemntation

function theme_custom_work_theme_field($variables) {
$element = $variables['element'];
$output = '';
$output .= drupal_render($element['bucket']['select_list']);
$output .= " "; // This space forces our fields to have a little room in between.
$output .= drupal_render($element['bucket']['textbox_fill']);
return $output;
}

// implement value callback function

function custom_work_bucket_element_value_callback($element, $input = FALSE, &$form_state) {
if ($input !== FALSE) {
if ($input['bucket']['select_list'] && !$input['bucket']['textbox_fill']) {
$input['bucket']['select_list'] = '';
}
if ($input['bucket']['textbox_fill'] && !$input['bucket']['select_list']) {
$input['bucket']['textbox_fill'] = '';
}
return $input;
}
elseif (!empty($element['#default_value'])) {
return $element['#default_value'];
}

return;
}

Comments

Chris Matthews’s picture

Version: 7.x-1.4 » 7.x-1.x-dev
Status: Needs work » Closed (outdated)