Entity reference field and dependent dropdown - drupal

I have a content type with an entity reference field referencing to a custom entity. I need to use a select box because an autocomplete widget is not suitable in my case. However, I cannot load all the entities at once as selectable values because they are too many (72000+ the form won't even load). So I default the entity reference select box to a limited number of values using a views filter and then hide it by default. Then I use an ajax dependent dropdown to show and populate the entity reference select box with filtered down values (I'm using a module that implements hook_form_alter).
My problem is that the form won't validate because now I can select entity reference values which are not the default ones in the select box. So I guess I should control in some way the validation rules of the entity reference field. Is there an easy way to do this? Which hook should I use?

Set the entity reference field to autocomplete and take it out of the process entirely in your form alter with $form['field_entity_ref']['#access'] = FALSE. This should fix the validation problem. (of course, "field_entity_ref" is what I'm calling your actual reference field.
Add your own validation to the form, if that is still necessary.
Finally, implement hook_node_presave() to manually put the value of your custom ajax drop down box.
So if your custom ajax select box was named my_custom_ref, then it would look something like this:
function mymodule_node_presave($node) {
if (isset($node->my_custom_ref)) {
$node->field_entity_ref[$node->language][0]['target_id'] = $node->my_custom_ref;
}
}

Related

Symfony forms ChoiceType how to add new choices from user's input

I am using js library that allows user to add new values into html select element but I cannot find a way handing that new values from symfony side and dynamically add new option.
As one of the solutions I was trying to use 'choice_loader' options with default ChoiceType field and implementing ChoiceLoaderInterface like in this post but there should be a simpler way.
I don't think you can do it in an other way with a ChoiceType but maybe you can create you own custom type :
You can create your select element by customize your form rendering or/and default classes set in your custom FormType.
You can use a ModelTransformer in order to retrieve your existing values and create new ones
You can use a simple TextType in inheritance format the data send by the form with a delimiter and create a ViewTransformer in order to retrieve a collection on the other side

Sitecore 6.5 change user control dynamically

I have a collection of some items. Using * symbol I set user control (ascx) in presentation details for all of them. Now I have a problem because on of this items has to be display in another control. Is there some trick that allow me to change used control dynamically, for example checking url segment?
I'm guessing you're using wildcard item called * with some presentation details defined on it. And now you want to display different components for one you the urls?
If you want to have completely different presentation, you can add another item as a sibling of the * item and put new presentation detail there. This item will be matched before the wildcard item, if the url segment is equal to this item name.
If you want to change only one or few components, you can use personalization for this component and where the item name compares to ... rule.
Marek's answer is preferable, but for completeness I will provide another potential option.
It depends on how you are handling the wildcards. I don't think it will work if you are using the wildcard item module from the Marketplace and it might not play well with some of your existing code, but here goes...
You could place the required presentation details on the target items themselves. Then when you resolve the wildcard, you would need to change the context item to be selected target item. When the page loads, it will use the presentation of the newly set context item.
One way to achieve this would be to create a custom item resolver
class WildCardItemResolver : ItemResolver
{
public override Process(HttpRequestArgsargs args)
{
base.Process(args);
// if Context.Item is as wildcard
// look up the target item
Context.Item = targetItem
}
}

Drupal 7: how to filter view content (with entity reference field) based on current page content

in my drupal 7 I have 2 content-types like these:
ContentA
ContentB (with a field Entity Reference to ContentA)
In the front-end detail page of ContentA, I would love to show a block/view with a list of ContentB whose Entity Reference field is set to the current ContentA.
I made a view of type Block and added it correctly to the page, but I cannot filter ContentB based on the current ContentA.
Could you help me?
Thanks
You should add a contextual filter for the value you will use for filtering to the block View of ContentB.
Then in your contextual filter in the "When the filter value is NOT in the URL" area select "Provide default value" and Type "PHP code" (You should have enable php filter for this). In your php code area you should have the following code
$node=menu_get_object();
return $node->field_your_machine_field_name['und'][0]['target_id']; // this is the field you will use for fitlering
Hope it helps
UPDATE
The above code will work if you need to show in your block similar results with the same selection (for example similar results of ContentB with the same selection in the referencing field of ContentA ).I will not delete because you might need it also in your project.I misunderstood. Actually the solution is simpler. You should add the contextual filter to the field and in "When the filter value is NOT in the URL" area select "Provide default value" and "Provide id from url"

How to show wordpress setting api validation error message?

I have created a setting api for my new plugin. It has four <input> field and one <select> option. In that field , user can change value according to their requirement. But it must be valid value. Suppose , one <input> field support only integer value. so when user will fill that <input> field by string or by other things, it will show an error message. Please tell me how can i do that? Can you suggest any tutorial ?
When using add_setting you can pass a sanitize_callback option as part of the array, this can contain the name of a function that is run which can contain your validation.
A similar method exists for JavaScript too, which you could use to validate the fields as well.
You asked for a tutorial too, here is one that outlines the method I've described: http://themeshaper.com/2013/04/29/validation-sanitization-in-customizer/

Use same MXML for an Add & Modify Form?

I have a form that is written in MXML that allows a user to create/add a User.
I need to add a form that allows a user to modify SOME but NOT ALL of the fields for this user.
The forms are so similar, I don't want to have to create two separate forms, one for Add and one for Modify.
For example, in the Add form, the user specifies a user id. In the Modify form, the "user id" field is not editable.
I'm wondering how I can initialize the MXML form (i.e. pass in a parameter?) so that it knows whether it is in the Add state or the Modif state.
I know I can't do the following but this is what I would like to do (pseudocode):
if (ADD_FORM) {
mx:TextInput id="txtUserID"
}
else {
mx:Label id="lblUserID"
}
This kind of thing is handled well with states. In Flex 3 you define your states like view, add, and edit. Then you can add the components that are common to all states to the document. Within each state declaration, you can then add the components that are specific to only some states. You can have the edit state dependent on add state since edit is add plus a few more fields.
In Flex 4 this is even easier. You declare your states, and then inline in the single document have all content for all states, with includeIn attributes for which states each element should be included in (or excludeIn).

Resources