-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add tags to Post #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tags to Post #447
Conversation
|
||
private function addRandomTags(Post $post) | ||
{ | ||
$indexes = (array) array_rand(LoadTagFixtures::$names, mt_rand(1, 3)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use 0 as the min number of tags ? Tags are optional, so it makes sense to have untagged posts too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
*/ | ||
class LoadFixtures extends AbstractFixture implements ContainerAwareInterface | ||
class LoadPostFixtures extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, you can remove the Load
part of the name, now that there is an entity name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
|
||
$newNames = array_diff($names, $tags); | ||
foreach ($newNames as $name) { | ||
$tag = new Tag(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ I'm not sure if it's the best place to do that (i.e. Inside a DataTransformer)
'Era brevis ratione est', | ||
'Sunt torquises imitari velox mirabilis medicinaes', | ||
'Cum mineralis persuadere omnes finises desiderium bi-color', | ||
'Bassus fatalis classiss virtualiter transferre de flavum', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added 15 phrases more to avoid duplicate post title and slug issue.
* | ||
* @author Yonel Ceruto <[email protected]> | ||
*/ | ||
class ArrayToStringTransformer implements DataTransformerInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yceruto, I would name this class like TagArrayToStringTransformer
, because the current name sounds very common.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it makes sense, thanks!
$names = explode(',', $string); | ||
} | ||
|
||
$tags = $this->manager->getRepository(Tag::class)->findBy([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yceruto, what about to define tags repository as service and inject it instead of the object manager?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the benefit :/ it could complicate things
@yceruto, you forgot to call |
AFAIK it's not necessary, Doctrine is able to detect the changes on M2M |
{ | ||
$builder | ||
->addModelTransformer(new CollectionToArrayTransformer()) | ||
->addViewTransformer(new TagArrayToStringTransformer($this->manager)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be a model transformer, not a view transformer. The TextType expects its normalized data to be a string. Changing it to be an array of tags could lead to issues (and such issues could appear in a future version of Symfony if some changes are made, even if there is no such issue today).
Changing the type of the normalized data when extending a type is generally not a supported usage of the type.
the code should look like this:
$builder
->addViewTransformer(new TagArrayToStringTransformer($this->manager))
->addModelTransformer(new CollectionToArrayTransformer())
;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, nice catch! fixed thanks!
web/js/main.js
Outdated
// Bootstrap-tagsinput initialization | ||
// http://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/ | ||
var $input = $('input[data-toggle="tagsinput"]'); | ||
var source = new Bloodhound({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be done only when $input.length
is not 0 (i.e. at least one such input has been found on the page)
{ | ||
$builder | ||
->addModelTransformer(new TagArrayToStringTransformer($this->manager)) | ||
->addModelTransformer(new CollectionToArrayTransformer()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At sight this looks confusing for newcomers now, looks like the data transformation is (even if it doesn't really happen so):
Tag[] -> String -> Collection -> Array
(i.e. following the order of addition)
What about other alternatives to do the same:
$builder
->addModelTransformer(new DataTransformerChain([
new CollectionToArrayTransformer(),
new TagArrayToStringTransformer($this->manager),
]))
;
or better
$builder
->addModelTransformer(new CollectionToArrayTransformer(), true)
->addModelTransformer(new TagArrayToStringTransformer($this->manager), true)
;
Does this make sense or does it simply not matter?
@stof, @voronkovich Thanks for the review. I have addressed your comments. |
@yceruto I'm really sorry it took me so long to merge this. I finally did it and I'll propose some tweaks in a separate PR. My only regret is that the solution looks too complex. Don't get me wrong, you did a great work and the feature it's fantastically implemented ... but thinking as a potential Symfony user looking at this demo application, I don't like such a complex solution for such a common feature. |
@javiereguiluz I would be happy to improve those parts where the solution is complex, please just tell me which ones and I'll try to propose a better one or comment the code for a better understanding of it. Thanks, best regards. |
@yceruto my comment was more about the complexity imposed by the Form component, such as creating a custom data transformer, enabling two data transformers in the custom form type, calling to the always confusing |
This PR was squashed before being merged into the master branch (closes #448). Discussion ---------- Tweaked the new blog post tags feature This continues #447 and introduces some minor changes. --- The only relevant changes for end users is the minor redesign of the tags. For example, in the blog index, before tags stand out too much and they look like buttons:  Now they are faded because, after all, they are a secondary information:  In the blog post show, the tags are bigger:  And in the admin, I haven't modified anything, so they still look like this:  Commits ------- f09c564 Tweaked the new blog post tags feature
This implements #44 and it's a complete variant of #192 (On hold since Ago 2016).
Also this splits the only one fixture class into three classes (
UserFixtures
,PostFixtures
andTagFixtures
):DependentFixtureInterface
(dependence between fixtures).Add Translation:
Updated
blog.sqlite
andblog_test.sqlite
DBs.Handling Tags (Bootstrap-tagsinput):
There is many options to do that:
select2
orchosen
js plugins?TODO: