Wordpress Divi Custom Module Visual Editor Error - wordpress

I am developing a custom Divi module and I have a problem with the fb_support my module loads and works perfectly except in the visual editor. When I put the variable $this->fb_support = true my module disappear in visual editor and get this JavaScript error.
bundle.js?ver=3.0.106:formatted:1422 Uncaught TypeError: Cannot read property 'lockedParent' of undefined
at A (bundle.js?ver=3.0.106:formatted:1422)
at Object.ID_2 (bundle.js?ver=3.0.106:formatted:3571)
at e._invokeCallback (bundle.js?ver=3.0.106:formatted:35300)
at e.dispatch (bundle.js?ver=3.0.106:formatted:35288)
at Object.moduleActive (bundle.js?ver=3.0.106:formatted:7203)
at t.value (bundle.js?ver=3.0.106:formatted:8759)
at Object.l (bundle.js?ver=3.0.106:formatted:24713)
at Object.invokeGuardedCallback (bundle.js?ver=3.0.106:formatted:28497)
at Object.invokeGuardedCallbackAndCatchFirstError (bundle.js?ver=3.0.106:formatted:28500)
at h (bundle.js?ver=3.0.106:formatted:24781)
I think this error is because i don´t have registered my plugin in JavaScript part. Exist any method to do this? Is the correct way to do this?
Here is the code of my plugin:
class Price_Manager extends ET_Builder_Module
{
function init ()
{
$this->name = 'Price Manager;
$this->slug = 'et_pb_prices';
$this->fb_support = true;
$this->whitelisted_fields = array(
'widget_title',
'widget_type',
'admin_label',
'module_id',
'module_class',
);
$this->main_css_element = '%%order_class%%';
$this->advanced_options = array(
'custom_margin_padding' => array(
'css' => array(
'important' => 'all',
),
),
'filters' => array(),
);
}
function get_fields ()
{
$fields = array(
'widget_title' => array(
'label' => 'Title',
'type' => 'text',
'option_category' => 'basic_option',
'description' => 'This is your widget title',
),
'widget_type' => array(
'label' => 'Type of Widget',
'type' => 'select',
'option_category' => 'basic_option',
'options' => array(
'company' => 'Company',
'residential' => 'Residential'
),
'description' => 'Here you can choose the type of widget.',
),
'admin_label' => array(
'label' => 'Admin Label',
'type' => 'text',
'description' => 'This will change the label of the module in the builder for easy identification.',
),
'module_id' => array(
'label' => 'CSS ID',
'type' => 'text',
'option_category' => 'configuration',
'tab_slug' => 'custom_css',
'option_class' => 'et_pb_custom_css_regular',
),
'module_class' => array(
'label' => 'CSS Class',
'type' => 'text',
'option_category' => 'configuration',
'tab_slug' => 'custom_css',
'option_class' => 'et_pb_custom_css_regular',
),
);
return $fields;
}
function shortcode_callback ($atts, $content = null, $function_name)
{
$module_id = $this->shortcode_atts['module_id'];
$module_class = $this->shortcode_atts['module_class'];
$widget_title = $this->shortcode_atts['widget_title'];
$widget_type = $this->shortcode_atts['widget_type'];
$module_class = ET_Builder_Element::add_module_order_class($module_class, $function_name);
$module_id = '' !== $module_id ? sprintf('id="%s"', esc_attr($module_id)) : '';
$module_class = '' !== $module_class ? sprintf('%s', esc_attr($module_class)) : '';
$content = 'My title: ' . $widget_title; // TODO: Fetch the item from DB and prepare HTML for output.
$content .= '<br />';
$content .= 'My type of widget: ' . $widget_type;
$output = sprintf(
'<div class="et_pb_prices_module_wrapper et_pb_module">
<div %1$s class="et_pb_prices%2$s et_pb_module">%3$s</div>
</div>',
$module_id,
$module_class,
$content
);
return $output;
}
}
new Price_Manager();
Thanks.

Related

Can't add a section to a Woocommerce Custom Setting Tab [duplicate]

I'm trying to add a custom settings tab to the WooCommerce settings screen. Basically I want to achieve a similar thing to the Products settings tab, with the subsections/subtabs:
I haven't been able to find any decent documentation on how to do this but I've been able to add a custom tab using this snippet:
class WC_Settings_Tab_Demo {
public static function init() {
add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
}
public static function add_settings_tab( $settings_tabs ) {
$settings_tabs['test'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
return $settings_tabs;
}
}
WC_Settings_Tab_Demo::init();
Based on what I've dug up from various threads/tutorials, I've been trying to add the sections/subtabs to the new settings tab something like this:
// creating a new sub tab in API settings
add_filter( 'woocommerce_get_sections_test','add_subtab' );
function add_subtab( $sections ) {
$sections['custom_settings'] = __( 'Custom Settings', 'woocommerce-custom-settings-tab' );
$sections['more_settings'] = __( 'More Settings', 'woocommerce-custom-settings-tab' );
return $sections;
}
// adding settings (HTML Form)
add_filter( 'woocommerce_get_settings_test', 'add_subtab_settings', 10, 2 );
function add_subtab_settings( $settings, $current_section ) {
// $current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:'';
if ( $current_section == 'custom_settings' ) {
$custom_settings = array();
$custom_settings[] = array( 'name' => __( 'Custom Settings', 'text-domain' ),
'type' => 'title',
'desc' => __( 'The following options are used to ...', 'text-domain' ),
'id' => 'custom_settings'
);
$custom_settings[] = array(
'name' => __( 'Field 1', 'text-domain' ),
'id' => 'field_one',
'type' => 'text',
'default' => get_option('field_one'),
);
$custom_settings[] = array( 'type' => 'sectionend', 'id' => 'test-options' );
return $custom_settings;
} else {
// If not, return the standard settings
return $settings;
}
}
I've been able to add new subsections to the Products tab using similar code to the above, but it isn't working for my new custom tab. Where am I going wrong here?
1) To add a setting tab with sections, you can firstly use the woocommerce_settings_tabs_array filter hook:
// Add the tab to the tabs array
function filter_woocommerce_settings_tabs_array( $settings_tabs ) {
$settings_tabs['my-custom-tab'] = __( 'My custom tab', 'woocommerce' );
return $settings_tabs;
}
add_filter( 'woocommerce_settings_tabs_array', 'filter_woocommerce_settings_tabs_array', 99 );
2) To add new sections to the page, you can use the woocommerce_sections_{$current_tab} composite hook where {$current_tab} need to be replaced by the key slug that is set in the first function:
// Add new sections to the page
function action_woocommerce_sections_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Must contain more than one section to display the links
// Make first element's key empty ('')
$sections = array(
'' => __( 'Overview', 'woocommerce' ),
'my-section-1' => __( 'My section 1', 'woocommerce' ),
'my-section-2' => __( 'My section 2', 'woocommerce' )
);
echo '<ul class="subsubsub">';
$array_keys = array_keys( $sections );
foreach ( $sections as $id => $label ) {
echo '<li>' . $label . ' ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
}
echo '</ul><br class="clear" />';
}
add_action( 'woocommerce_sections_my-custom-tab', 'action_woocommerce_sections_my_custom_tab', 10 );
3) For adding the settings, as well as for processing/saving, we will use a custom function, which we will then call:
// Settings function
function get_custom_settings() {
global $current_section;
$settings = array();
if ( $current_section == 'my-section-1' ) {
// My section 1
$settings = array(
// Title
array(
'title' => __( 'Your title 1', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_1'
),
// Text
array(
'title' => __( 'Your title 1.1', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 1.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_1_text',
'css' => 'min-width:300px;'
),
// Select
array(
'title' => __( 'Your title 1.2', 'woocommerce' ),
'desc' => __( 'Your description 1.2', 'woocommerce' ),
'id' => 'custom_settings_1_select',
'class' => 'wc-enhanced-select',
'css' => 'min-width:300px;',
'default' => 'aa',
'type' => 'select',
'options' => array(
'aa' => __( 'aa', 'woocommerce' ),
'bb' => __( 'bb', 'woocommerce' ),
'cc' => __( 'cc', 'woocommerce' ),
'dd' => __( 'dd', 'woocommerce' ),
),
'desc_tip' => true,
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_1'
),
);
} elseif ( $current_section == 'my-section-2' ) {
// My section 2
$settings = array(
// Title
array(
'title' => __( 'Your title 2', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_2'
),
// Text
array(
'title' => __( 'Your title 2.2', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 2.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_2_text',
'css' => 'min-width:300px;'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_2'
),
);
} else {
// Overview
$settings = array(
// Title
array(
'title' => __( 'Overview', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_overview'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_overview'
),
);
}
return $settings;
}
3.1) Add settings, via the woocommerce_settings_{$current_tab} composite hook:
// Add settings
function action_woocommerce_settings_my_custom_tab() {
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::output_fields( $settings );
}
add_action( 'woocommerce_settings_my-custom-tab', 'action_woocommerce_settings_my_custom_tab', 10 );
3.2) Process/save the settings, via the woocommerce_settings_save_{$current_tab} composite hook:
// Process/save the settings
function action_woocommerce_settings_save_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::save_fields( $settings );
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $tab_id . '_' . $current_section );
}
}
add_action( 'woocommerce_settings_save_my-custom-tab', 'action_woocommerce_settings_save_my_custom_tab', 10 );
Result:
Based on:
Implement a custom WooCommerce settings page, including page sections
woocommerce/includes/admin/settings/

How can I render Nested elements on Visual Composer (WPBakery) for wordpress?

I'm trying to do some custom elements for Visual Composer plugin (WP-Bakery) for wordpress.
I have no problem with simple custom elements, but I'm trying to do some nested elements (a parent containing some child elements). I have no problems creating child elements, and if I create them alome, they are shown on wordpress, but when I try to create parent element, I can see setting elements without problem, but it's no rendered.
I think the problem is the render function (html) on parent class, but I can't get solve it.
PARENT CLASS
<?php
class vcInfoCardContainer extends WPBakeryShortCodesContainer {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'vc_infocardcontainer_mapping' ) );
add_shortcode( 'vc_infocard', array( $this, 'vc_infocardcontainer_html' ) );
}
// Element Mapping
public function vc_infocardcontainer_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('VC Info Card Container', 'ex5_vc_elements'),
'base' => 'vc_infocardcontainer',
'description' => __('Info Card Container for VC', 'ex5_vc_elements'),
'category' => __('Ex5 Elements', 'ex5_vc_elements'),
'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
'as_parent' => array('only' => 'vc_infocard'),
'is_container' => true,
'js_view' => 'VcColumnView',
'params' => array(
array(
'type' => 'textfield',
'heading' => __('Button text','ex5_vc_elements'),
'param_name' => 'button_text',
'description' => __('Default is \'Más info\'', 'ex5_vc_elements'),
'group' => 'Button',
)
),
));
}
//render
public function vc_infocard_html( $atts, $content = null ) {
// Params extraction
extract(
shortcode_atts(
array(
),
$atts
)
);
$html = '<div class="ex5-vc-info-card-container">' . do_shortcode($content) . '</div>';
return $html;
}
}
new vcInfoCardContainer();
CHILD CLASS
<?php
class vcInfoCard extends WPBakeryShortCode {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'vc_infocard_mapping' ) );
add_shortcode( 'vc_infocard', array( $this, 'vc_infocard_html' ) );
}
// Element Mapping
public function vc_infocard_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('VC Info Card', 'ex5_vc_elements'),
'base' => 'vc_infocard',
'description' => __('Info Card for VC', 'ex5_vc_elements'),
'category' => __('Ex5 Elements', 'ex5_vc_elements'),
'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
'as_child' => array('only' => 'vc_infocardcontainer'),
'params' => array(
array(
'type' => 'attach_image',
'heading' => __( 'Main image', 'ex5_vc_elements' ),
'param_name' => 'image',
'group' => 'Images',
),
array(
'type' => 'attach_image',
'heading' => __( 'Icon', 'ex5_vc_elements' ),
'param_name' => 'icon',
'group' => 'Images',
),
array(
'type' => 'colorpicker',
'heading' => __( 'Icon background color', 'ex5_vc_elements' ),
'param_name' => 'icon_background_color',
'value' => __( '#000000', 'ex5_vc_elements' ),
'group' => 'Images',
),
array(
'type' => 'textfield',
'heading' => __('Title','ex5_vc_elements'),
'param_name' => 'Title',
'group' => 'Texts',
),
array(
'type' => 'textfield',
'heading' => __( 'Text', 'ex5_vc_elements' ),
'param_name' => 'text',
'group' => 'Texts',
),
array(
'type' => 'checkbox',
'class' => 'one-third',
'heading' => __( 'Show link button', 'ex5_vc_elements' ),
'param_name' => 'show_button',
'value' => 'show',
'description' => __( 'Indicates if link button is shown)', 'ex5_vc_elements' ),
'group' => 'Button',
),
array(
'type' => 'textfield',
'heading' => __('Button text','ex5_vc_elements'),
'param_name' => 'button_text',
'description' => __('Default is \'Más info\'', 'ex5_vc_elements'),
'group' => 'Button',
),
array(
'type' => 'vc_link',
'heading' => __( 'Button link', 'ex5_vc_elements' ),
'param_name' => 'button_link',
'group' => 'Button',
),
),
));
}
//render
public function vc_infocard_html( $atts ) {
// Params extraction
extract(
shortcode_atts(
array(
'image' => '',
'icon' => '',
'icon_background_color' => '#000000',
'title' => '',
'text' => '',
'show_button' => '',
'button_text' => 'Más info',
'button_link' => '',
),
$atts
)
);
if (empty($button_text)) $button_text = __( 'Más info', 'ex5_vc_elements' );
if ($show_button === 'true') {
if (!empty($button_link)) {
$button = '<div class="ex5-vcic-button">
<a href="'. $button_link .'" target="_self" class="ex5-vcic-link" title="' . $button_text . '">
<span class="ex5-vcic-button-text">' . $button_text . '</span>
</a>
</div>';
} else {
$button = '<div class="ex5-vcic-button">
<span class="ex5-vcic-button-text">' . $button_text . '</span>
</div>';
}
} else {
$button = '';
}
$image = wp_get_attachment_image_src($image);
$icon = wp_get_attachment_image_src($icon);
//vc_build_link(
$html = '
<div class="ex5-vc-infocard">
<div class="ex5-vcic-content">
<div class="ex5-vcic-image">
<span>
<img src="' . $image[0] . '" title="history_inner_14" alt="http://oxigeno.">
</span>
</div>
<div class="ex5-vcic-icon" style="background-color: ' . $icon_background_color . '">
<img src="' . $icon[0] . '" />
</div>
<header class="ex5-vcic-headline">
<h3>' . $title . '</h3>
</header>
<div class="ex5-vcic-text">
<p>' . $text . '</p>
</div>' .
$button
. '</div>
</div>';
return $html;
}
}
new vcInfoCard();
There was a problem with the name of container shortcode. It had to be
add_shortcode( 'vc_infocardcontainer', array( $this, 'vc_infocardcontainer_html' ) );
But there's still a problem. I have a problem with do_shortcode_tag function
Attempting to parse a shortcode without a valid callback
Anybody knows how can I solve it?
I had solved it. The shortcode call was wrong because it had the wrong function name too.
public function vc_infocard_html( $atts, $content = null ) {
must be
public function vc_infocardcontainer_html( $atts, $content = null ) {

Wordpress / CMB2 Framework / Display data from repeatable fields in frontend

I have a own option-page in the backend-sidebar with this repeatable fields in it. (not a metabox) I cant figure out, how to display the data from the group field in the frontend.
function ww_register_theme_options() {
$ww_prefix = '_ww_';
$ww_contacts = new_cmb2_box( array(
'id' => $ww_prefix . 'ww_option_plugin',
'title' => esc_html__( 'Ansprechpartner', 'ww-contact' ),
'icon_url' => '/wp-content/plugins/ww-contact/assets/images/icons/ww-icon-white.png',
'object_types' => array( 'options-page' ),
'option_key' => 'ww_options',
) );
$ww_group_field = $ww_contacts->add_field(array(
'id' => $ww_prefix . 'contact_repeat_group',
'type' => 'group',
'description' => __('Ansprechparter Liste', 'ww-contact'),
'repeatable' => true,
'options' => array(
'group_title' => 'Ansprechpartner {#}',
'add_button' => __('neuer Ansprechpartner', 'ww-contact'),
'remove_button' => __('Entfernen', 'ww-contact'),
'sortable' => true,
),
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'forname',
'type' => 'text',
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'surname',
'type' => 'text',
));
}
Remove the prefix from your add_group_field ids they aren't needed. Then loop through like so...
$entries = get_post_meta( $parent, 'ww_contact_repeat_group', true );
if($entries){
foreach ( (array) $entries as $key => $entry ) {
if ( isset( $entry['forname'] ) ) {
$forname = $entry['forname'];
}
}
}

How to use 'custom_markup' parameter in Visual Composer 'vc_map'?

I was wondering if anyone have a clue about using custom_markup parameter in Visual Composer? I've came across with a solution in this link but it seems outdated.
I'm using:
WordPress 4.8
Visual Composer 5.1
Any help will be appreciated. Thanks.
FYI the gist link provided in your question has been updated and his solution is working perfectly with Wordpress 4.9.2 and WPbakery Page Builder (formerly Visual Composer) 5.4.5
js :
(function($) {
window.VcCustomElementView = vc.shortcode_view.extend( {
elementTemplate: false,
$wrapper: false,
changeShortcodeParams: function ( model ) {
var params;
window.VcCustomElementView.__super__.changeShortcodeParams.call( this, model );
params = _.extend( {}, model.get( 'params' ) );
if ( ! this.elementTemplate ) {
this.elementTemplate = this.$el.find( '.vc_custom-element-container' ).html();
}
if ( ! this.$wrapper ) {
this.$wrapper = this.$el.find( '.wpb_element_wrapper' );
}
if ( _.isObject( params ) ) {
var template = vc.template( this.elementTemplate, vc.templateOptions.custom );
this.$wrapper.find( '.vc_custom-element-container' ).html( template( { params: params } ) );
}
}
} );
})(window.jQuery)
php :
vc_map(array(
'name' => __( 'Custom Markup', 'js_composer' ),
'base' => 'vc_custom_markup',
'category' => array(
__( 'Content', 'js_composer' ),
),
'description' => __( 'Custom markup element', 'js_composer' ),
'params' => array(
array(
'type' => 'textfield',
'param_name' => 'style',
'value' => 'custom style!',
'heading' => 'Style',
),
array(
'type' => 'textfield',
'param_name' => 'color',
'value' => 'custom color!',
'heading' => 'Color',
),
array(
'type' => 'textfield',
'param_name' => 'title',
'value' => 'custom title!',
'heading' => 'Title',
),
),
'js_view' => 'VcCustomElementView',
'custom_markup' => '<div class="vc_custom-element-container">Style: "{{ params.style }}", Color: "{{ params.color }}", Title: "{{{ params.title }}}"</div>',
)
);
All credits go to AngeIII

Drupal 7 adding custom menus to pages

I have been developing with Drupal 7 for the past 4 months now, and I can't seem to find a straight answer as to how to add more menus on my pages. I understand the whole system_main_menu and system_secondary_menu, but how in the world can I add more menus to my page if I make a custom menu, let's say I have a footer_social_menu? I just love dynamic menus.
Here's what I am working with right now
function cornmaze_links($variables){
$html = '<ul>';
foreach($variables['links'] as $link){
$html .= '<li>'. l($link['title'], $link['href'], $link).'</li>';
}
$html .= '</ul>';
return $html;
}
I tried using the THEME_links($vars) function, but that affects ALL of the menus, what if I wanted to add a certain ID to a custom menu? or change the custom menu to use all divs? That's what I don't get. I can't necessarily loop through the menus using the THEME_links() function?
I don't want to put them in a block either, if I don't have to, just to avoid any extra markup that I don't need. I just want to be able to control menus, whether they be system or custom.
Any help, or light shed would be awesome! Thank you in advance!
Try menu block module. It creates your menus as blocks and highly configurable.
Here's the documentation link.
Please Check , this may be help ful for you,
function formuserentry_menu() {
$items = array();
$items['formuserentry'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Entry Page',
'page callback' => array('formuserentry_view'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
$items['formuserentry/application'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Entry Application Forms', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('formuserentry_application' , 2), //put the name of the form here
'access arguments' => array('administer your module'),
);
return $items;
}
/********** front page view ***********/
function formuserentry_view() {
$result = 'My Sub Menu URL was hit';
$header = array('Entry Id','Name', 'DOB', 'Year', 'Image' );
$rows = array();
$no_yes = array('No', 'Yes');
$results = db_query("SELECT * FROM userentryform ORDER BY userentryId DESC");
foreach ($results as $node) {
$rows[] = array(
l($node->firstname, 'formuserentry/application/'. $node->userentryId ),
array('data' => $node->firstname, 'class' => 'title'),
array('data' => $node->lastname, 'class' => 'type'),
array('data' => $node->birthyear, 'class' => 'type'),
array('data' => '<img src="sample.jpg">dff', 'class' => 'image'),
);
}
return theme('table', array('header' => $header, 'rows' => $rows));
}
/********************** add form ***************************/
function formuserentry_application($form, &$form_state, $candidateId) {
$firstname = '';
$lastname = '';
$birthyear = '';
/****** query fetch ******/
if(isset($candidateId)){
$fetchquery = db_select('userentryform', 'n')
->fields('n', array('firstname','lastname', 'birthyear'))
->fields('n')
->condition('userentryId',$candidateId)
->execute()
->fetchAll();
$firstname = $fetchquery[0]->firstname;
$lastname = $fetchquery[0]->lastname;
$birthyear = $fetchquery[0]->birthyear;
}
/**************************/
//print($fetchquery);
//drupal_set_message('<pre>'. print_r($fetchquery[0]->firstname, TRUE) .'</pre>');
$form['name'] = array(
'#type' => 'fieldset',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['name']['first'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
'#default_value' => $firstname,
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['canid'] = array(
'#type' => 'hidden',
'#required' => FALSE,
'#default_value' => $candidateId,
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#value' => $lastname,
'#required' => TRUE,
);
$form['name']['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => "Year of birth",
'#description' => 'Format is "YYYY"',
'#value' => $birthyear,
'#required' => TRUE,
);
// Image upload field.
$form['name']['image'] = array(
'#type' => 'managed_file',
'#title' => 'File',
'#upload_location' => 'public://my-files/',
'#process' => array('formuserentry_my_file_element_process'),
"#upload_validators" => array('file_validate_is_image' => array())
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
/********* for validation ************/
> function formuserentry_my_file_element_process($element, &$form_state,
> $form) { $element = file_managed_file_process($element, $form_state,
> $form); $element['upload_button']['#access'] = FALSE; return
> $element; }
>
> function formuserentry_application_validate($form, &$form_state) {
> $year_of_birth = $form_state['values']['year_of_birth'];
> if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
> form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
> } }
/********** form submission ***************/
function formuserentry_application_submit($form, &$form_state) {
$first_name = $form_state['values']['first'];
$last_name = $form_state['values']['last'];
$year_of_birth = $form_state['values']['year_of_birth'];
$profileimage = $form_state['values']['image'];
$canid = $form_state['values']['canid'];
if($canid == '') {
$nid = db_insert('userentryform')
->fields(array(
'firstname' => $form_state['values']['first'],
'lastname' => $form_state['values']['last'],
'birthyear' => $form_state['values']['year_of_birth'],
'profileimage' => $form_state['values']['profileimage'],
))
->execute();
drupal_set_message(t('The form has been Added your last insert ID is => '.$nid));
} else {
$nid = db_update('userentryform')
->fields(array(
'firstname' => $form_state['values']['first'],
'lastname' => $form_state['values']['last'],
'birthyear' => $form_state['values']['year_of_birth']
))
->condition('userentryId',$canid)
->execute();
drupal_set_message(t('The form has been Updated your last insert ID is => '.$nid));
}
drupal_goto("/formuserentry/");
}

Resources