How to get the value of the form field in drupal 7? - forms

I have added a field on the page_node_form with the following code.
switch($form_id){
case 'page_node_form':
$form['comment_popup'] = array(
'#type' => 'select',
'#title' => t('Comments popup'),
'#options' => array('On' => 'On', 'Off' => 'Off'),
);
}
As the comment form appears on the node so I want when the comment_popup field has the value Off then the subject field should not be displayed in the comment form and when the comment_popup field has the value 'On' then the subject field should be displayed.
I have tried the following code but it did not work.
case 'comment_node_page_form':
if($form_state['comment_popup']['#value'] == 'Off') {
$form['subject']['#access']=FALSE;
}
Any one who can help me?

What you're doing can't work I'm afraid, you're setting an element in one form and then trying to read it from another form which doesn't make sense.
What you need to do is add a submit handler to the page_node_form function which saves the comment_popup value for that node to a custom table. Then in the form alter for `comment_node_page_form' you need to read in the comment_popup data for that particular node, and make the access decisions to the form elements based on that.
Hope that helps

Related

How to use/link sys_category field in custom model in TYPO3

I am developing an extension in which I am uploading files and for each file upload I need to have one or more categories associated with it.
I have built a custom category model and it shows fine at the backend when creating a record, but I want to show/link the sys_category records instead of my own custom categories.
How do I link that field in my custom model?
If anyone else stumbles upon this, I found the solution from the documentation thanks to #larry-pete.
Simply add these lines to your ext_tables.php file in your extension folder.
// Add an extra categories selection field to the pages table
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
'ext_key',
'your_table_name',
'categories',
array(
// Set a custom label
'label' => 'LLL:EXT:ext_key/Resources/Private/Language/locallang.xlf:additional_categories',
// This field should not be an exclude-field
'exclude' => FALSE,
// Override generic configuration, e.g. sort by title rather than by sorting
'fieldConfiguration' => array(
'foreign_table_where' => ' AND sys_category.sys_language_uid IN (-1, 0) ORDER BY sys_category.title ASC',
),
// string (keyword), see TCA reference for details
'l10n_mode' => 'exclude',
// list of keywords, see TCA reference for details
'l10n_display' => 'hideDiff',
)
);
Hope it helps someone.

How to add custom subpanel on detail view page without relationship

How to add custom subpanel on detail view page without relationship
Eg.: I want to add Accounts subpanel on Leads Detail View Page.
This might give you an edge towards the correct answer but you should be aware that it is fairly involved.
You can create a custom dashlet by adding a file to the director custom/Extension/modules/Leads/Ext/Layout/name_this_file_anything.php
The content of the file should be something add a new element to the array $layout_defs['Leads']['subpanel_setup']. You can probably find what to add from the layout defs files in the existing modules directory.
If you need to create a custom subpanel that is quite a bit more work and involves creating a function called "getSubpanelQueryParts($params)" which queries the required records and returns an array of query parts and adds an element to the subpanel_setup/custom_dashlet array which reads something like this:
'collection_list' => array(
'calls_opportunities' => array(
'subpanel_name' => 'ForAccounts',
'module' => 'Calls',
'get_subpanel_data' => 'function:getSubpanelQueryParts', // here custom method defined
'generate_select' => true, // to build custom SQL query
'function_parameters' => array(
'import_function_file' => 'custom/application/Ext/Utils/custom_calls_opportunities.php',
'return_as_array' => 'true'
), // to get data for subpanel collection item
),
),

How to perform add action in a form only one time, after update the record everytime

I have a form element. When I click the submit button it will add the data to the table.
that is working fine. But my need is to add data to the table only the first time, after each submission data should updated.how we can do this?
function add(){
if(!empty($this->data)){
$this->loadModel('Defineroute');
$this->Defineroute->create();
$this->Defineroute->save($route_data); //route_data is data from the form
}
}
I have one more doubts, I have a view.ctp in the controller I have a view() function for that. But in the view.ctp I use one form element.
How we can write function for that element in the controller? When I try to write function, it shows the error missing view..
CakePhp determines if it should update an existing record or insert a new record based on the presence of the primary key ('id') inside the data you're saving;
This will insert a new record:
$route_data = array(
'Defineroute' => array(
'name' => 'I am a new record'
)
);
$this->Defineroute->save($route_data);
And this will update an existing record;
$route_data = array(
'Defineroute' => array(
'id' => 123,
'name' => 'I will update record with ID 123'
)
);
$this->Defineroute->save($route_data);
To accommodate this in a single form, create the form inside your view/element and only add an input for the id field if you're editing an existing record
Also see Saving your data

A set of fields for one property entity in Symfony 2

My Product entity has the following structure:
private $id;
private $title;
/**
* #ManyToOne(targetEntity="Category")
* #JoinColumn(name="cat_id", referencedColumnName="id")
*/
private $category;
Category have nested structure. And each level of nesting is shown in 5 separate fields:
In class form code, I solve it in this way:
$builder
->add('cat_1', 'entity', array(
...
'query_builder' => function() { return someSelectLogic1(); }
))
->add('cat_2', 'entity', array(
...
'query_builder' => function() { return someSelectLogic2(); }
))
->add('cat_3', 'entity', array(
...
'query_builder' => function() { return someSelectLogic3(); }
))
->add('cat_4', 'entity', array(
...
'query_builder' => function() { return someSelectLogic4(); }
))
->add('cat_5', 'entity', array(
...
'query_builder' => function() { return someSelectLogic5(); }
))
Now I need to know which field is filled in the last turn and pass the value of that field in the entity property.
In all that I do not like:
complex logic to determine which field with category was filled at the end
each of these fields is not tied to the entity 'mapped' => false
1) What the right way to organize code of my form?
2) And is there a way to bring these fields into a separate class which will deal with the logic of determining which category was chosen in the end?
I would suggest the following:
1) Create a new custom form field type and put all those entity in there.
This process is not much different from ordinary creation of form type. Just enclose those fields in it's own buildForm() and that should do the trick. Docs.
2) Mark all those entity fields with property "property_path => false".
Clearly you wont be storing these values inside your model.
3) Add two more fields: chosen and lastOne.
Now, this might be tricky: I would either set the chosen to text type (basically, generic type) or would use entity as well. If you go for entity you would need to include all possible answers from all entity fields. As for the lastOne set it to text as it will reflect which field (by name) was selected last.
Either way, those two fields will be invisible. Don't forget to set property_path to false for lastOne field.
4) Finally, add ValueTransformer (docs) which will contain logic to "see" which field was selected last.
Now, I dealt with it only once and don't understand it just quite yet, so your best bet would be trial and error with examples from official docs, unfortunately.
What basically you should do is to, within value-transformer, read the value of field lastOne. This will give you the name of field which was selected last. Then, using that value, read the actual last value selected. Last, set that value (object, if you've went for entity type, or it's ID otherwise) to chosen field.
That should basically do the thing.
As for the JS, I don't know if you're using any framework but I will assume jQuery. You will need to set lastOne field as your selecting items in your form.
$(function(){
$('#myform').find('select').on('change', function(){
var $this = $(this);
$this.closest('form').find('#__ID_OF_YOUR_LASTONE_FIELD').val($this.attr('name'));
});
});
I'm sorry I cannot provide you with code samples for PHP right now. It's a bit late here and will do my best to further update this answer tomorrow.

Selecting multiple checking boxes with FormHelper in CakePHP

So in an Index view for a certain model, I'm including some checkboxes; I have an array of days of the week, and so have 7 checkboxes available, per record.
I can save data, no problem. What I can't figure out is how to pre-select the checkboxes, based on each record's saved data. Currently I'm having to do this:
<?php if ( isset($user['SurveyAssignment'][0]['active_days']['Monday']) && $user['SurveyAssignment'][0]['active_days']['Monday'] == 1 ) { $monChecked = true; } else { $monChecked = false; } ?>
<?php echo $this->Form->input('SurveyAssignment.' . $count .'.active_days.Monday', array('type' => 'checkbox', 'label' => false, 'div' => false, 'checked' => $monChecked));?>
And that's just for ONE day, for one record. Right now, I'm doing that 7 times, for each record. It's very bloated.
I would have thought I could just read in the array for each record, and select a group of checkboxes accordingly.
I'm sure I'm missing something very simple but I can't see past my nose at the moment.
If your associations are set up correctly, your field name is correct, and you're passing the correct data, they will be selected automatically for you.
The likely answer is you're not using the correct field or Model.field for your form input.