{"action":"created","issue":{"url":"https://api.github.com/repos/ThatGuyCND/d8-twig-ext-example/issues/1","labels_url":"https://api.github.com/repos/ThatGuyCND/d8-twig-ext-example/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/ThatGuyCND/d8-twig-ext-example/issues/1/comments","events_url":"https://api.github.com/repos/ThatGuyCND/d8-twig-ext-example/issues/1/events","html_url":"https://github.com/ThatGuyCND/d8-twig-ext-example/pull/1","id":69259555,"number":1,"title":"Cleanup for current D8 standards and other general PHP style conventions","user":{"login":"itafroma","id":277116,"avatar_url":"https://avatars.githubusercontent.com/u/277116?v=3","gravatar_id":"","url":"https://api.github.com/users/itafroma","html_url":"https://github.com/itafroma","followers_url":"https://api.github.com/users/itafroma/followers","following_url":"https://api.github.com/users/itafroma/following{/other_user}","gists_url":"https://api.github.com/users/itafroma/gists{/gist_id}","starred_url":"https://api.github.com/users/itafroma/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/itafroma/subscriptions","organizations_url":"https://api.github.com/users/itafroma/orgs","repos_url":"https://api.github.com/users/itafroma/repos","events_url":"https://api.github.com/users/itafroma/events{/privacy}","received_events_url":"https://api.github.com/users/itafroma/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-18T02:56:59Z","updated_at":"2015-04-26T19:43:20Z","closed_at":"2015-04-26T19:43:20Z","pull_request":{"url":"https://api.github.com/repos/ThatGuyCND/d8-twig-ext-example/pulls/1","html_url":"https://github.com/ThatGuyCND/d8-twig-ext-example/pull/1","diff_url":"https://github.com/ThatGuyCND/d8-twig-ext-example/pull/1.diff","patch_url":"https://github.com/ThatGuyCND/d8-twig-ext-example/pull/1.patch"},"body":"The extension will almost work as is, with three minor tweaks. A lot of this is stuff we talked about in chat, but I figured i'd consolidate it here for reference.\r\n\r\nFirst, your extension class should extend `\\Twig_Extension`, not `Drupal\\core\\Template\\TwigExtension`. Core's `TwigExtension` is its own thing, and not meant to be a wrapper layer for contrib Twig extensions.\r\n\r\nSecond, adding the `trim_string()` filter to `{{ label }}` in `node.html.twig` is too late: by that point, the label variable has all the baggage of the renderable output from `theme_field()`. Instead, it should be placed in the field template for the node label, [`field--node--title.html.twig`](https://api.drupal.org/api/drupal/core!modules!node!templates!field--node--title.html.twig/8):\r\n\r\n```twig\r\n<span{{ attributes }}>\r\n  {%- for item in items -%}\r\n    {{ item.content | trim_string(3) }}\r\n  {%- endfor -%}\r\n</span>\r\n```\r\n\r\nThird, even here the function isn't going to get a plain string. What it will get is an array in `$string` that contains a single key, `#markup`. Modify that and return the full string array to get the full output.\r\n\r\nThis PR accounts for those things, but also makes the following stylistic changes to better match what core code/documentation is doing and other general PHP conventions:\r\n\r\n- While it can be anything (and will work with anything), the current convention is to name services like `module.twig.extension_name`, so `trim_string.twig.trim_string`.\r\n- I shortened the class name to `TrimString`: it's already namespaced to include `TwigExtension` preventing any collision issues and it's never used without the full namespace, so there shouldn't be any confusion as to which TrimString this is (core's `twig_extension_test` does the same).\r\n- I used `preg_split()` instead of `explode()` to account for more word break scenarios (e.g. if a user accidentally types multiple spaces instead of one).\r\n- I added the boilerplate docblocks used in `twig_extension_test`.\r\n- In the docblock for `TrimString::trimString()`, I changed `string[] $suffix` to `string $suffix`: `string[]` indicates that the parameter is an array of strings, which `$suffix` is not.\r\n\r\nFinally, instead of having to use a procedural function (and thus be forced to have a `.module` file), you can use a static method in the extension class (`twig_extension_test` does this too).\r\n\r\nIt used to be (especially in Drupal 7 and earlier) that when something required a callback/callable as a parameter, you were stuck with a string that mapped to a function (e.g. `twig_trim_string_filter`).\r\n\r\nHowever, since this is a normal PHP class and not a Drupal-specific construct, you can use an [array to call methods on objects](http://php.net/manual/en/language.types.callable.php): `array('Foo', 'bar')` when passed as a callable is equivalent to `Foo::bar()`. In this case, `array($this, 'trimString')` calls the static method `trimString()` within the same class (i.e. `Drupal\\trim_string\\TwigExtension\\TrimString::trimString()`)."},"comment":{"url":"https://api.github.com/repos/ThatGuyCND/d8-twig-ext-example/issues/comments/96425911","html_url":"https://github.com/ThatGuyCND/d8-twig-ext-example/pull/1#issuecomment-96425911","issue_url":"https://api.github.com/repos/ThatGuyCND/d8-twig-ext-example/issues/1","id":96425911,"user":{"login":"ThatGuyCND","id":1129934,"avatar_url":"https://avatars.githubusercontent.com/u/1129934?v=3","gravatar_id":"","url":"https://api.github.com/users/ThatGuyCND","html_url":"https://github.com/ThatGuyCND","followers_url":"https://api.github.com/users/ThatGuyCND/followers","following_url":"https://api.github.com/users/ThatGuyCND/following{/other_user}","gists_url":"https://api.github.com/users/ThatGuyCND/gists{/gist_id}","starred_url":"https://api.github.com/users/ThatGuyCND/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ThatGuyCND/subscriptions","organizations_url":"https://api.github.com/users/ThatGuyCND/orgs","repos_url":"https://api.github.com/users/ThatGuyCND/repos","events_url":"https://api.github.com/users/ThatGuyCND/events{/privacy}","received_events_url":"https://api.github.com/users/ThatGuyCND/received_events","type":"User","site_admin":false},"created_at":"2015-04-26T19:43:20Z","updated_at":"2015-04-26T19:43:20Z","body":"Changes applied, and it's working. thanks @itafroma :)"}}