Getting random path hierarchy for CCK fields inside form array - drupal

I have created a custom entity and i'm using CCK fields. Each bundle has it's own fields. For example:
function MYMODULE_install() {
// Check if our field is not already created.
if (!field_info_field('field_myField')) {
$field = array(
'field_name' => 'date_field',
'type' => 'list_text',
);
field_create_field($field);
}
//Enable is executed only once.
function bundle_callback_enable() {
// Create the instance on the bundle.
$instance = array(
'field_name' => 'date_field',
'entity_type' => 'payment_method',
'label' => 'Expiration Date',
'bundle' => 'card',
'required' => TRUE,
'settings' => array();
field_create_instance($instance);
}
My bundles are created from individual modules, so in each install file i am creating the respective fields.
Yesterday i tried to add validation callback functions in those fields and i saw something weird inside form array. Fields with type="text" had the path:
$form[field_name]['und'][0][value] //<! expectable
but fields with type='list_text' had only the path:
$form[field_name]['und'] //<! unexpectable
I couldn't find any solution and i've solved it with this:
function &get_cck_path_value( $field_name, &$form_path) {
$field = null
if ( isset( $form_path[$field_name][LANGUAGE_NONE] ) ) {
$field = &$form_path[$field_name][LANGUAGE_NONE]
}elseif(isset($form_path[$field_name][LANGUAGE_NONE][0])) {
$field = &$form_path[$field_name][LANGUAGE_NONE][0]['value'];
}
return $field;
}
I don't like this approach. Is too hucky. Can you tell me if that is a cck feature or bug?
I can't understand when it decides where to put the values( All the process is fulfilled through the "field_attach_form( ... )" )?
Have you faced any problem like this?
Thanks in advance.
Thandem.

I believe that you are seeing the abbreviated form field in validation because the field had no value entered into it and no default value was defined for it. There is no value, so no array is present to store the value.

Related

Sonata admin export fields with collection fields

I'm trying to make custom columns for export, but I can't access children. Is there any possibility to do that ?
My code at this moment looks like this:
public function getExportFields()
{
return [
'ID' => 'id',
'Transaction number' => 'transactionNumber',
'Loan account' => 'loan',
'Loan name' => 'loan.name',
'Amount' => 'amount',
//'Amount ($)' => '',
'Transaction type' => 'transactionCategory',
'Reference' => 'transactionAssociation.cashTransaction.transactionNumber',
'Date' => 'date'
];
}
I can't find out a solution. I was thinking to use PropertyAccess, but I don't know how to integrate it here.
I'm using Symfony 3.X with Sonata.
To get the collection records in export you cannot directly do this by specifying the property with association, A workaround for to achieve this you can define a new unmapped property in your entity with a getter function which will get all the collection details like in your main entity define new property as
protected $cashTransactionNumber;
public function getCashTransactionNumber()
{
$cashTransactionNumber = array();
$i = 1;
foreach ($this->getTransactionAssociation() as $key => $transactionAssociation) {
$cashTransactionNumber [] = $i .
') No.:' . $transactionAssociation->somemethod()->__toString()() .
/** Other properties */;
$i++;
}
return $this->cashTransactionNumber = join(' , ', $cashTransactionNumber );
}
then in your getExportFields() method call this property
public function getExportFields(){
return array(
'Reference'=>'cashTransactionNumber ',
....// Other properties
);
}
Reference: Exporting one to many relationship on sonata admin

Adding elements to a field widget in Drupal 8

What I need to accomplish is adding a link to a node to field widgets (the node contains the formatted instructions to properly compile the field).
So far I've been able to add the node reference field in the field config form, using Third Party Settings:
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id === 'field_config_edit_form' && $form_state->getFormObject()->getEntity()->get('entity_type') == 'myentity') {
$field = $form_state->getFormObject()->getEntity();
$help_page_id = $field->getThirdPartySetting('mymodule', 'help_page_id');
$form['help_page_id'] = array(
'#type' => 'entity_autocomplete',
'#title' => t('Help page'),
'#target_type' => 'node',
'#selection_handler' => 'default',
'#selection_settings' => array(
'target_bundles' => array('help_page'),
),
'#default_value' => $help_page_id ? Node::load($help_page_id) : NULL,
'#weight' => 100,
);
$form['#entity_builders'][] = 'mymodule_form_field_config_edit_form_builder';
}
}
Now I'm having troubles retrieving this information when showing the form, and altering the field widget in order to display the link.
Using hook_field_widget_form_alter, I cannot get the custom configuration value from the arguments I have:
function mymodule_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
}
I guess I have to load the field configuration entity, but I don't know how.
I didn't test it, but something like this should work:
function mymodule_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
// Add a suffix to widget form elements for all fields of type entity_reference.
$field_definition = $context['items']->getFieldDefinition();
if ($field_definition->getType() == 'entity_reference') {
// TODO: Render the node link to $link
$element['#suffix'] = $link;
}
}

Drupal fields: Passing a variable to view

I have a custom field plugin. It's widget looks somewhat like this:
function mymodule_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
...
$main_widget = $element + array(
'#test' => 'test',
'#type' => 'textarea',
'#attributes' => array('item_capacity' => 3),
'#theme'=>'mymodule_theme'
);
...
}
I need to get a value of another field, attached to current node, and pass it to this widget's theme file. Can i somehow get at least an id of a node that contains current field from hook_field_widget_form()?
You can use the module Devel to print the value of the $form array which will contain the form information you are looking for.
Example:
dpm($form);
You will be able to easily see the content of the form and access it with php in array form.

Map array to entity in Symfony2/Doctrine2

I'm using the DoctrineFixtures bundle to create example entities during development. In my ORM fixtures load() method, I define the data as associative arrays and create the entity object in a loop.
<?php
// ...
public function load($manager) {
$roleDefs = array(
'role-1' => array(
'role' => 'administrator'
),
'role-2' => array(
'role' => 'user'
),
);
foreach($roleDefs as $key => $roleDef) {
$role = new Role();
$role->setRole($roleDef['role']);
$manager->persist($role);
$this->addReference($key, $role);
}
$manager->flush();
}
I always use the same array schema. Every array element uses the property name (in underscore notation) of the entity as index. If the entity structure becomes more complex, there are a lot of $entity->setMyProperty($def['my_property']); lines.
I think the problem of mapping propertynames to setter methods is a very common problem in Symfony and Doctrine as this type of mapping is found in many situations (e.g. mapping forms to entities).
Now I'm wondering if there is a built-in method that can be used for mapping. It would be nice to have a solution like
foreach($defs as $key => $def) {
$entity = $magicMapper->getEntity('MyBundle:MyEntity', $def);
// ...
}
Has someone an idea how this can be achieved?
Thanks a lot,
Hacksteak
I sometimes use loops when creating fixtures. I'm not sure if this solution fits your requirements, but I find that the most flexible way to build fixtures and quickly add new properties over time if you need is to do the following... Assuming the creation of a bunch of blog posts:
// an array of blog post fixture values
$posts = array(
array(
'title' => 'Foo',
'text' => 'lorem'
'date' => new \DateTime('2011-12-01'),
),
array(
'title' => 'Bar',
'text' => 'lorem'
'date' => new \DateTime('2011-12-02'),
),
// more data...
);
// loop over the posts
foreach ($posts as $post) {
// new entity
$post = new Post();
// now loop over the properties of each post array...
foreach ($post as $property => $value) {
// create a setter
$method = sprintf('set%s', ucwords($property)); // or you can cheat and omit ucwords() because PHP method calls are case insensitive
// use the method as a variable variable to set your value
$post->$method($value);
}
// persist the entity
$em->persist($post);
}
This way you can add more properties by just adding the new values to your array.

Drupal hook_form_alter anonymous user can't see the fields

I created a small module for altering forms called "form_mods". The form I'm altering is the "user_profile_form". I added a category for extra fields called "Profile".
I created a select field in the Drupal admin called "profile_state" and I'm altering it in my module to have a key => value list of states and It's working for me when logged in as an admin but an anonymous user that's trying to register sees an empty states select field. Is there a permissions issue here? I tried to add 'access' => user_access('access content') to the field but that didn't work. Here is my code:
function form_mods_form_alter($form, $form_state, $form_id) {
switch ($form_id) {
## user_profile_form ###################################################################################
case 'user_profile_form': // This is our form ID.
//echo '###'.$form['_category']['#value'].'###';
if($form['_category']['#value'] == 'Profile'){
// Modify the states dropdown list
$states = load_states_list();
$form['Profile']['profile_state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#options' => $states,
'#required' => TRUE,
'#default_value' => isset($form['Profile']['profile_state']['#default_value']) ? $form['Profile']['profile_state']['#default_value'] : '',
'#element_validate' => array('profile_state_validate')
);
}
####################################################################################
break;
}
}
function load_states_list() {
$states = array('' => '-- Select a state'); // add a default empty value
$results = db_query("SELECT * FROM {states_list} ORDER BY name ASC");
while ($state = db_fetch_array($results)) {
$states[$state['code']] = $state['name'];
}
return $states;
}
Thanks
First of all, are you sure you're function ever gets run? Having the function named the way you do, I don't think it is. If the module name is "form_mods", your function should be called
function form_mods_form_alter
Or...since you're only modifying the user_profile_form form, you could use the function name
function form_mods_user_profile_form_alter
Now, the reason it isn't working is because you don't have the & in front of the $form in the parameter list. The & basically passes the variable as reference, and so any changes you make to the $form variable will be saved and passed back to the calling function. So, your function should look like
function form_mods_form_alter(&$form, &$form_state, $form_id) {
OR
function form_mods_user_profile_form_alter(&$form, &$form_state) {
Reference: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_alter/6
Thank you mikesir87 for the reference link. I figured out my problem.
There are 2 different forms that are using those fields I created. They have different form id's. I have to look for "user_profile_form" and "user_register" form id's.
Here is my new code that works:
function form_mods_form_alter($form, $form_state, $form_id) {
if( (($form_id == 'user_profile_form') && ($form['_category']['#value'] == 'Profile')) || ($form_id == 'user_register') ){
// Modify the states dropdown list
$states = form_mods_load_states_list();
$form['Profile']['profile_state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#options' => $states,
'#required' => TRUE,
'#default_value' => isset($form['Profile']['profile_state']['#default_value']) ? $form['Profile']['profile_state']['#default_value'] : ''
);
}
}
thanks

Resources