Is OOP really helping? - oop

I am trying to create a simple module in Drupal 8 and it looks like what was really easy in Drupal 7 is now unbelievably complicated. It can be done with help of StackOverflow and Google, however, one example:
Getting field values from node like this:
'name' => $node->get('title')->getValue(),
'body' => $node->get('body')->getValue(),
'image' => file_create_url($node->field_fotografia->entity->getFileUri()),
Why is the way of getting image field different from the title and body? How do I know what way to use and how will I know in future if there is any other way for other type of field?

the logic is quite simple
$node->get('body')->getValue();
means: get property value of field "body"
D7 equivalent is
$node['body'][$language][$index]['value];
which is straigh forward
now the other example
$node->field_fotografia->entity->getFileUri()
means: get property uri of entity stored in field "field_fotografia" since the image is not a property value but a separate entity; actualy I believe that $node->get('field_fotografia')->getValue() returns the image id which make sense. Same model applies for all entity references (multifield, paragraphs, other nodes, etc).

Related

Algolia vue-instantsearch : disjunction between two distincts facets

Using algolia vue-instantsearch, i’m encountering a special case and i’m having an hard time finding a solution.
Refinement behaviors is that you get the results that matches all your refinement filters.
If i filter on a brand and a price, i’ll get the results that matches both the brand and the price.
I need to add some specific filters that work differently. I would like to be able to say “returns me the results that matches either refinementA, or refinementB, or refinementC.”
The reason is that those refinements are checking fields that are not present on all the products.
If i check a value for refinementA, i want to keep all the results that has no value for field corresponding to refinementA, but remove those that has a value for refinementA that does not match with the one i filtered one.
Im thinking about handling myself some inputs instead of ias-components, and modifying by hand each query that is send to algolia by checking the value of my own inputs when searchFunction is triggered (https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/#widget-param-searchfunction).
I did not found yet how i can trigger a search from an non-vue-instantsearch input event, and i’m not sure how the above solution could affect the internal working of vue-instantsearch.
I’m looking for feedbacks about a more standard way of doing this, or for any advices !
I got the answer by exchanging with a vue-instantsearch maintainer.
vue-instantsearch does not provide any option to do it.
A workaround is to overwrite algoliasearch search method, that is used under the hood by vue-instant-search.
const client = algoliasearch('', '');
const originalSearch = client.search;
client.search = function search(queries) { ... }
More informations in this github issue : https://github.com/algolia/vue-instantsearch/issues/1102

MVC multiple parameter routing

I've been trying to pull the parameters passed into a page so I can post it back in Context.
So far,
ViewBag.Message = string.Format("{0}::{1}::{2}",
RouteData.Values["controller"],
RouteData.Values["actions"],
RouteData.Values["id"]);
works with anything simple like "66" or "tt" but anything more complex like "?name=blargh?viewId=66" and it fails.
I've tried a bunch of different ways to see if I could strike gold but nothing seems to work so does anybody have any idea what I'm missing/doing wrong/should be doing instead?
" but anything more complex like "?name=blargh?viewId=66" and it fails.
This doesn't seem to be routing information but query string which you should retrieve from the Request.QueryString bag.
If the {id} parameter is part of your route (as the default routes {controller}/{action}/{id}) I hope you realize that this id cannot be anything you like just because there are rules for an url. For example it cannot contain ? because this symbol has an entirely different meaning in an url - it represents the query string separator.

How to assign unique id attribute to each table row of a CGridView?

I am attempting to assign a unique id to each table row in Yii's CGridView.
Preferably something like $data->id from the database table.
I have been unsuccessful at adding an id attribute to each rendered <tr>.
Any suggestions would be most appreciated.
CGridView have an option called 'rowHtmlOptionsExpression' , you can declare like the followings to assign row an id
'rowHtmlOptionsExpression' => 'array("id"=>$data->id)',
It's better than hacking into 'rowCssClassExpression'
Good luck !
Modern solution (since Yii 1.1.13)
This is now possible to do using the rowHtmlOptionsExpression attribute, which allows assigning arbitrary HTML attributes to each rendered table row. For example:
'rowHtmlOptionsExpression' => '["id" => $data->id]'
Original answer (earlier versions)
Not directly possible because CGridView does not support it, but there are a couple of straightforward solutions that you can try.
Subclass CGridView (good)
Simply create your own class MyGridView extends CGridView and override the renderTableRow method to spit out ids on every row. Have a look at the stock implementation, which does for the class attribute exactly what you 'd like to do for the id attribute.
Use a CSS class instead (not so good)
Speaking of class attributes, the rowCssClassExpression property can be used to dynamically generate classes out of the box. IMHO this is a bad workaround, but it's there.
You could extend CGridView to add that functionality.
or be a bit hacky with rowCssClassExpression.
'rowCssClassExpression' => '\'" data-id="\' . $data->rowID'
Try the information I posted here:
How to set key value in CGrideView when grid is populated from table-view
In essence, as long as your dataprovider to the CGridview provides the data->id in a form that it understands, it will auto handle the $data->id stuff for you automatically so that it's easily available to javascript.
CGridView.rowHtmlOptionsExpression is undefined
I don't think that we can use rowHtmlOptionsExpression

Ruby on Rails 3: rails_admin + puret?

Did someone try to integrate puret into rails_admin? I can't make a language switch to edit different translations :(
Changing I18n.locale forces whole rails_admin to use specified locale.
Now I got the solution. The two can work together well. In short:
Delete the pureted column(s) in your model
If you have the column pureted still in your model, rails form helper will bypass puret. That is, if a model called Post has a field called contents to be i18ned, the table posts SHOULD NOT have the column contents.
Actually we should use globalize3 instead. With this you do not need to remove the original column. And puret doens't support nested attributes assignment. globalize3 works very well.

Mongoid: Changing the order of documents in an embeds_many relation

I have a mongoid document which embeds other documents with a relation like
this:
embeds_many :blocks
Creating new blocks works fine, but I cannot manage to change the
order of existing embedded documents. For example I have three
embedded blocks and I want to move the last one to the first
position.What's the correct way to do that?
I had to deal with this with mongoid's recursively_embeds_many feature, but it's essentially the same. There's nothing wrong as far as I can tell with literally rewriting the document. Write a model method to do something like:
def reverse_blocks
reversed_blocks = blocks.to_a.reverse
blocks.clear
reversed_blocks.each do |b|
blocks.create b.attributes
end
save
end
That's not great code above, but it gives you an idea of how to do what you want to do. I'm not thrilled with having to go through that just to reorder stuff in an array, but there it is.
I think, that really correct way is make in your embedded docs field "weight" and query them with asc(:weight) or desc(:weight). You don't rely on the order of persisted non-embedded docs, so you shouldn't in embedded.
But if you urgently need to make this, your embedded docs in mongoid are just array, so you can do such way:
doc.embedded_docs = [doc.embedded_docs.last] + doc.embedded_docs[0..-2]