I'm attempting to dynamically load a select box's options based on some existing criteria in my database. The useful information is that there are 3 classes, a Pet, which has a breed and type, a Breed, which has a name and type, and Type which is something like 'Dog' or 'Cat'.
When editing a pet I have two drop-downs populated from Doctrine/Symfony2.
Type [Select One]
Breed [Select One]
Currently the Breed select lists all breeds of dogs and cats, I'd like to select the 'Type' and have that force the Breed select box to only list Breeds based on that type.
I've done this with raw php/javascript before using Symfony figured there's some best practice for handling it in Symfony.
Any advice would be appreciated. I can provide any additional information needed, but wanted to keep it as simple as possible.
I didnt found any other way than a .change jquery on the first select, then an ajax call with the selected option to repopulate the target select.
$('#Your_source_select').change(function()
{
$.ajax({
type: 'POST',
url: Routing.generate('AJAXDevisOnduleurOptions', { onduleurId: $('#Your_source_select').val() }),
success: function(msg){
$('#Your_target_select').children().remove().end().append(msg);
}
});
});
Please note that Your_source/target_select are entity fields.
But it seems that you got that part.
You might want to use FOSJSRoutingBundle => https://github.com/FriendsOfSymfony/FOSJsRoutingBundle
It allows you to build route in your javascript using twig syntax. So it allows you access to its variables.
Related
I have an entity category, which has a code. This code is internal, and we use translate it for each language. for exemple, imagine this:
Categories:
---- id:1 Code: "Bread"
---- id:2 Code: "Butter"
I have a form with a form field entity. I want to order it by translated label.
In English for exemple, it will display
Bread
Butter
But in french for exemple, the order is different
Beurre (butter)
Pain (bread)
So I can't use the orderBy of the entity field.
I have a hand-made solution, very dirty: I use an choice field with translated label
$categories_translated =array();
$categories= $this->em->getRepository('MyRepo')->findAll();
foreach($categories as $category){
$categories_translated[$category->getId()]= $this->translator->trans($category);
}
asort($categories_translated);//sorted
//then later
$builder->add('category','choice',array( 'choices' => $choices_technologies) )
Do you have a proper way to do this?
Your way is the best (and I think the only) way to handle this issue with file based translations. There are indeed more possibilities to address problems like this with database related translations. For example: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/symfony2.md
Are the codes static? i.e. the oven-baked dough (whatever language) is always id=1 in your data dictionaries, then the sorting is fine.
Hypothetically, if you had a table as follows:
category
- catid int auto_increment,
- locale varchar(5),
- itemid int,
- text varchar(100)
One can do select to access to categories sorted by text.
If one just rasterises according to the relevant locale and wanted items, you get global ids for your categories.
You may wish to use a better i18n mechanism, but this would be very simple for static categories.
Context: Content type Person has reference (multiple values) to a content type Work, using entity reference.
Need: To display the title of each person node which references a given work, separated by a comma.
Done: A view with a back reference, the right nodes are fetched. (Views 7.x-3.7)
Problem: Cannot display the value separated by a comma. Note: I usually do it with the "Simple separator" display type which is under "Display all values in the same row" in the MULTIPLE FIELD SETTINGS field group. However, this field group is not available in my context.
Solved
I have found the module Views Merge Rows - works very nice. If it does not support Features module for some reason, I can take some of its code code in order to use hook_views_pre_render myself.
I was able to work around this problem by using token_formatters. The basic steps (after token formatters is installed):
No relationship to referenced entity in views (not needed)
Add the entity reference field to the view
Change formatter to "tokenized text"
For 'Text to output' use a token (I'm using [node:field-name])
For 'link destination' use a token ('m using [entity:url:path] for a relative link)
Set multiple field setting as desired
You need a custom views Format here because you are talking about the whole views-row not a multiple results field. You can use the "Unformatted list" and add a comma to be added with CSS or JS.
What kind of Relationship do you use? Can you export your whole views in an external editor and provide a link?
I had a similar issue, where I was using the Entity Reference relationship of "Referencing Entity" instead of "Referenced Entity". (The reference was on the child and the View started at the parent level).
When you run a Drupal System Message on the row (dsm), it returns all the nid responses appropriately, but as different result rows instead of as a single object; however, since the NID field (like many others) has no option for display multiple results, it would only grab the first result.
I ended up having to do an Entity Query from a Views PHP field with the current row's NID as one of the Field Conditions. That seemed to do the trick, rather than trying to load a View inside of a View with views_field_view.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', '[your_content_type]')
->propertyCondition('status', 1)
->fieldCondition('[your_field_machine_name]', '[field_column_to_check]', $row->nid)
->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
I had a very similar problem: no "Multiple Field Settings" were available in the field configuration of a multi-value entity reference from my content type to User.
Solved it by removing the entity reference and instead using the multi-value "User ID" field of my content type directly. The "Multiple Field Settings" form area was available now and I selected "Display all values in the same row" there as you do normally. Now this would only display numeric user IDs separated by comma (not desired). But in the field configuration there was also a setting "Format:", which I set to "Label". This would display user names instead.
So I guess by creating a custom formatter you would be able to display your associated "Work" entities in a similar way.
Scenario
I have a form type that I use both for creation or edit of mine entities. These entities have some kind of relationship with other entities in a x-to-many fashion. That means that - relatively to save action -my dropdown list will be populated with list of constrained entities.
Suppose that - in form - whe have something like this:
Principal Entity: MiniCooper
List of Accessories
Product 1: [list of input properties that user can insert and save directly with this form]
Product 2: [list of input properties that user can insert and save directly with this form]
.....
Product n: [list of input properties that user can insert and save directly with this form]
Where Product[1,2,....,n] are the "many" side of relationship (consider "principal entity" the "x" side of relationship; it doesn't matter for our example if, actually, this should be an n-to-m relationship) and suppose that these related entities are "automatically" assigned to my principal entity on creation of "principal entity" itself.
When I render my form - through an entity type - I'll obtain that Product 1 , Product 2 , .... , Product N will be render as dropdown lists with the corrisponding element selected.
But..... I don't want this behaviour. I'll prefer that they could be some kind of "label" or something like that (So no one can change the selection and produce multi-assignment of the same "Product"). How can I write my entity type - now these fields are "entity" typed - to obtain this?
Moreover, have I to use mandatorily the Data Transformer ??
Screenshot
As you can see from screenshot, all dropdownlist (apart "price type") shouldn't be dropdown (i know that I can make one-value dropdown but i don't want them).
Hope this image will help you to understand better my issue.
I'll prefer that they could be some kind of "label" or something like that (So no one can change the selection and produce multi-assignment of the same "Product")
From what you describe it sounds as though you want to display the data, rather than provide a form to edit it (that is, you want to list the entity and its accessories on the page).
The way to tackle this is to avoid forms altogether, and just make the entities available in the view and render the information directly.
Alternatively if you are attempting to represent in a form 'multiple choice' amongst related items in a (for simplicity's sake) 1-to-many relationship, the 'expanded' and 'multiple' options for the 'entity' form field type should do what you want.
If neither of these is the answer, then it'll be because I'm struggling to grasp the question :).
I have a page which should list nodes. The views is called from a locality page (a taxonomy term page). What I need is almost the same as using the Taxonomy: tid in arguments and passing the tid.
I can't use the term_node table, as (for other reasons) we have a custom table term_node_hierarchy (with nid and tid only). The table term_node_hierarchy is like term_node but also saves the tid of the parents (from an "external" code)
I've been looking for options but still no joy.
Currently I'm building an array of the nid's that should be displayed on the current page, and passing them like print views_build_view('embed', $view, $matching_nids); but the Argument Node: ID states This argument is a single Node ID. As said, only the first node is displayed when printing the views. It would be great if it could filter on more than one nid.
I'm open to any kind of suggestions on how to do this.
Thanks
You could create your own module for this. You could populate the $page_content variable with the results of your own custom query where you allow the user to sort against multiple nids. You could do this a number of different ways. You could display a list of the existing nids with corresponding checkboxes, so that, when the user clicks submit, all the nids that match the selected checkboxes get used in the query. Then you just display the result of that query. That's the easiest way I can think of to offer that degree of flexibility.
I'm using Drupal's Views 2, and need to retrieve min values from fields in a custom table. The query for this is easy if I were writing it by hand--something like, "SELECT foo, min(bar) FROM table GROUP BY foo." But how would I do this using Views? I've already defined the table in a views.info file, so there's no trouble getting views to see the table. It's the Min() part of the query I just don't understand. My next stop will be the Views API documentation, but if someone can just provide the outline for how to do this quickly, I would greatly appreciate it.
New answer to an old question, but something like this will work. You need to create a custom field handler and then wrap the field as follows:
class views_handler_custom_field extends views_handler_field {
function query() {
$this->ensure_my_table();
$this->field_alias = $this->query->add_field("MAX({$this->table_alias}", "{$this->real_field})",$this->table_alias . "_" . $this->real_field);
}
}
Use aggregation from advanced configuration of views. After this is set
yes you can select max, min or any other selector to fields.
Test your results but it should work well
Alternatively in some cases you can sort your data ascending or
descending and then just pick one to be shown on the view. Can be
problematic when displaying multiple fields or so.
After testing first one seems to be faster at least on small scale.
One could consider the modules groupby and views_calc, but I assume they are not acceptable for you.
Additionally, you can accomplish this with a custom module.