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.
Related
Let me start from scratch to describe what I need:
I have a website about games and I want to get some kind of database, so that players can search/filter it (by year, by genre, by platforms, etc.).
What I came to: I store the games in a separate post-type 'games', for them I added to ACF (Advanced Custom Fields) a group of fields with the data I needed (Release date, Genre, Platform, etc.).
I want to do what is called a "game list" (you know various lists Games about pirates, Games about space, tons and tons of lists, so no - it is not category): this is most likely a post in which I want to be able, for example, to do this:
<post content>
Some useful text ...
<h2> Some Headline </ h2>
// this can be a shortcode or block in guttenberg, I'm not very good. specialist
[insert-game-info id = "1"]
Just a couple paragraph of cool text useful for SEO
[insert-game-info id = "2"]
Lyalyalya, and still text, and behind him again game
[insert-game-info id = "3"]
</ post content>
How to make such a thing? It is clear that it is very necessary that there be some kind of dialogue that allows you to choose from the list of games.
I'm researching whether there is already solution, for me it seems that it must be common problem
Depending on your proficiency, I would recommend using the WordPress get_posts function and return the field data with ACF's get_field inside of the foreach.
ACF's get_field allows you to get a value from a specific post like so:
$value = get_field( "text_field", 123 );
In Views, I have an exposed filter that looks at the UID (User ID / Author), but is there a way to limit this to "ONLY" the users who have posted in this content type?
I tried adding a "Content: Author" relationship and hit Apply. I'm not exactly sure why, but it wasn't until this point that I could go back into the relationships and see MORE options, like: "User: Content authored" (which must then be dependent on the first relationship?) so I selected that one too and set it up like so:
Now I was able to go to the exposed filter and select the relationship:
But this didn't work-- the exposed filter continues to show all the registered users.
I also tried putting in a User Reference field (to this content type) and attaching that to a relationship, but it didn't allow anything to show and gave this SQL warning:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_hidden_name.uid' in 'on clause'
How can I limit this author exposed filter to "ONLY" the users who have posted in this content type?
There are a few drupal modules that may help you out. Such as Corresponding node references
However, if you are good with php code, you can create the logic to query the database and return the desired results (which would be the columns of the table). Create a contextual filter, then select a field (any field should work, but best select something in either content type). Then edit it, WHEN THE FILTER VALUE IS NOT AVAILABLE -> Provide default value -> PHP Code
$nid = arg(1);// current node id
$query = "SELECT <desired_field_column_name> FROM {<field_data_table_name>} c
WHERE c.<column_that_is_same_as_nid> = :nid";
$result = db_query($query, array(':nid' =>$nid));
$id = array();
while ($row = $result->fetch())
{
array_push($id, $row->field_curator_target_id);//put each node id that's referenced in array
}
$separated = implode("+", $id); // separate them by + for AND , for OR
return $separated; //eg.32 + 30 would represent nodes that you want.
In Linked theme there is answer about Views Selective Filters (aka "Views Selective Exposed Filters", aka views_filters_selective, aka views_selective_filters).
This module adds filters that have suffix "(selective)" in their names – add one you need instead of filter w/o the suffix.
I have three content types: Artist, Artwork, Exhibition. An Exhibition has a field 'artworks' (unlimited values). An Artwork has a field 'artist' (1 value, required).
And there is the relation I can't seem to find with Views: I want all Exhibitions an Artist ever participated in. Which means: on the Artist page, show all Exhibitions all Artworks of this Artist were ever in.
The problem (I think) is that one field (Exhibition.artworks) has many values. Yet Artwork.artist has just 1 value.
I don't know what the problem is =) but it's not working and I've tried a million things. At this point, I'll accept writing SQL queries, but the drupal content database is so incredibly untransparent that I have no idea what to query and how.
Obviously I'd be happiest with an unhacked Views solution, but I'm not getting my hopes up. Anyone experience with relations like this?
You can build dependent relationships that should help you to accomplish this. Use a relationship (Artwork) on exhibition.artworks and a relationship (Artist) on (Artwork).Artist
It would be easier to understand what you're doing with exports of the views & content types.
The database structure for content types in Drupal works as follows;
The node is the base table, with nid as index. Your content types have their own tables, content_type_XXXXXX with all single entry fields (that aren't shared among content types) members of that table. Multiple entry and shared fields get their own table content_field_XXXXXX. All of the tables relate on the nid field, and multiple entry fields use a "delta" to indicate the entry order.
i have a content type event with following fields date,type and using fivestar module for voting. The type takes 3 possible values 'art', 'entertainment', 'iq'. i try to generate a block that should display top event (by votes) in each category. any one have idea ??
You should be able to do this relatively easy in a custom module, I have a hard time seeing how you would do this in views with the UI.
You need a query that looks something like this
SELECT nid FROM {voting_api} AS v
LEFT JOIN {content_content_type} AS c on v.content_id = c.nid
WHERE c.field_name = 'art'
AND v.function = 'count'
AND c.content_type = 'node'
ORDER BY v.value
LIMIT 1;
You need to run a query for each value, art, entertainment and iq. If you want to make it more reliable, you should use content_fields() and content_database_info() to get the table name and column name of your CCK field (which can change over time).
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.