I'm trying to make a form with some fields and save the values in Drupal 7 where every user can enter their data. I tried creating a custom entity with an existing database table like so:
function bank_info_entity_info() {
return array(
'bank_info' => array(
'label' => t('BankInfo'),
'entity class' => 'Entity',
'base table' => 'bank_info',
'field_ui_base_route'=> 'banking',
'controller class' => 'EntityAPIController',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'user_uid',
'"bundle"' => 'bank_info',
),
// Use the default label() and uri() functions
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
//'module' => 'bank_info',
),
);
}
and I keep getting this error:
array_keys() expects parameter 1 to be array, null given common.inc:7334
PHP Warning: Invalid argument supplied for foreach() in /var/www/html/includes/common.inc on line 7314
when I run the command:
drupal_get_schema('bank_info')
It returns false which causes that error.
Any ideas?
Related
WordPress allows registering meta values in the REST API through register_meta().
One can add a schema to the meta like so:
register_meta(
'user',
'my_meta_key',
array(
'type' => 'array',
'description' => 'This is my array meta key!',
'single' => true,
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
'format' => 'date-time',
),
),
),
)
);
It took me some time to find out about the 'format' => 'date-time' option.
I would like to validate my meta fields myself (for a custom post-type). But it seems like the WP_REST_Posts_Controller uses get_endpoint_args_for_item_schema() to set up the schema for arguments in register_rest_route().
register_rest_route() would allow for a custom validate_callback, but rest_get_endpoint_args_for_schema() hard-codes it to rest_validate_request_arg(), which uses rest_validate_value_from_schema().
And there are the primitives and hard-coded formats hex-color, date-time, email, ip and uuid.
So there are two options I see:
Live with it. Just accept the validating against schema primitives, since the schema is only validated on REST requests.
Extend WP_REST_Posts_Controller just to sneak in my own get_endpoint_args_for_item_schema(), which adds an exception for my meta key.
Do you have another idea, how to solve this issue?
EDIT: There seems to be a pattern property for strings. So I could at least do something like (don't mind the regex itself):
'schema' => array(
'type' => 'string',
'pattern' => '[0-9]/[0-9]^$',
),
i have created module but unablet to create table below is my code of mysubscribers.install
mysubscribers.install
function mysubscribers_schema(){
$schema['mysubscribers'] = array(
'description' => 'The table for storing the subscriber data.',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for subscriber.',
'type' => 'serial',
'not null' => TRUE,
'unsigned' => TRUE,
),
),
'primary key' => array('id'),
);
return $schema;
}
/**
* Implements hook_install().
*/
function mysubscribers_install() {
if (db_table_exists('mysubscribers')) {
db_drop_table('mysubscribers'); // old table from 5.x-2.x
}
}
No issues here when running your code.
You don't need to remove the DB on install.
If you make changes to the schema add your changes to hook_update()
with something like db_change_field() or db_add_field().
As long as your naming convention is correct this should work.
assuming you have a mysubscribers.info,mysubscribers.module, and mysubscribers.install
I'm trying to add custom fields to the user registration form. I need to save 2 of them into the Drupal User's database and 1 into my own separate database.
I'm using hook_form_alter to add the fields and then hook_form_submit to save the fields.
How do I submit the first/last name fields into drupal's user table and then save the SSN into my own? I have a function to switch between the 2 databases already but am just not sure how to use hook_submit_form to save some in one database and some in another?
function myModule_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'user_registration_form'){
$form['f_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
'#attributes' => array('class' => array('fname'))
);
$form['l_name'] = array(
'#type' => 'textfield',
'title' => t('Last Name'),
'#required' => TRUE,
'#attributes' => array('class' => array('lname'))
);
$form['ssn'] = array(
'#type' => 'textfield',
'#title' => t('Social Security Number')
'#maxlength' => 11,
'#required' => TRUE,
'#attributes' => array('class' => array('ssn'), 'placeholder' => '999-99-9999')
);
Would it be something like this?
function myModule_form_submit($form, &$form_state){
//Array of information to save into drupal user's table
$edit = array(
'f_name' => $form_state['values']['f_name'],
'l_name' => $form_state['values']['l_name'],
'second_email' => $form_state['values']['second_email'],
);
user_save(drupal_anonymous_user(), $edit);
drupal_set_message(t('The form has been submitted.'));
}
I'm not sure if I can use drupal's user_save() function and pass it this new information to save or if I have to do something in the .install file to add these new columns and then target them in the form_submit?
In what field will you save the custom field data? You should create a custom table for those fields and save data in there. When you load the user, you should also load those details from that table. There is no way you can directly load those custom field data when user object is loaded.
I have a difficult problem to solve. I need to implement a functionality to create custom forms (that is declare what fields it should have) and then be able to save and retrieve the data. The form generation mechanism is based on EAV model - form template entity has form fields entity (which are form templates attributes). Each form field has type, name, label etc.
Then I dynamically display the form( foreach $fields as $field (...) $formBuilder->add($field->getType() etc.). After all that the data is saved in another entity called FormInstanceData which consist of field name:field value pairs.
The hard part is that I have to be able to create form templates with form field groups which behave like collections (new ones can be added with JS). The form (generated with the template) displays correctly, but I have problem with retrieving the data (as the final data is not an entity for obvious reasons). Simple fields can be successfully filled out with retrieved data (by passing data option with the field name as key), but I can't get the nested collection fields to work - even after passing the data, the collections simply don't display.
The part of the code responsible for that looks like this:
elseif ($fieldType === 'collection'){
$subfields = $field->getSubfields();
$formBuilder->add('subfields', 'collection', array(
'type' => new FormCollectionType($subfields),
'allow_add' => true,
'mapped' => false,
'allow_delete' => true,
'by_reference' => false,
'options' => array('required' => false, 'data' => array(
array('title' => 'lorem', 'subtitle' => 'ipsum'),
array('title' => 'lorem', 'subtitle' => 'ipsum')
The FormCollectionType is also generated dynamically with the $subfields parameter. In this case, each item in collection has two fields - title and subtitle. With the data I passed, two already filled out input groups should appear, but nothing does. You can still add new (empty) groups with JS.
Please advise.
Ok, turned out the data needs to be passed not as:
'options' => array('required' => false, 'data' => array(
array('title' => 'lorem', 'subtitle' => 'ipsum'),
array('title' => 'lorem', 'subtitle' => 'ipsum')
but:
'data' => array(
array('title' => 'lorem', 'subtitle' => 'ipsum'),
array('title' => 'lorem', 'subtitle' => 'ipsum')
$instance = array(
'field_name' => $field_name,
'entity_type' => $entity,
'bundle' => $bundle,
'field types' => 'list_boolean',
'widget' => array(
'type' => 'options_onoff',
'settings' => array('display_label' => 1)
),
'default_value' => array(array('value' => 1)),
);
this is not taken, and i have to save it twice in the admin contenttype - field/edit,
until it takes it ...
i now exported the finished field with the features module,
and took the generated code - suddenly it works, with default_value
i guess i was missing the property module on the field, also field types is inexistant ..
In your field definition, you have to set the allowed_values in the settings array in order for the default_value in the instance to get picked up.
so like this assuming you are doing this in a module
$fields[] = array(
'field_name' => '$field_name',
'type' => 'list_boolean',
'settings' => array(
'allowed_values' => drupal_map_assoc(range(0, 1)),
),
);
Instead of using 'default_value', I got it to work by using 'default_value_function' and creating a function that returns array(array('value' => 1)).