1 | <?php |
||
14 | class RouteController extends ControllerBase { |
||
15 | |||
16 | /** |
||
17 | * @var \Drupal\graphql\GraphQL\Execution\QueryProcessor |
||
18 | */ |
||
19 | protected $queryProcessor; |
||
20 | |||
21 | /** |
||
22 | * @var \Drupal\Core\Template\TwigEnvironment |
||
23 | */ |
||
24 | protected $twig; |
||
25 | |||
26 | /** |
||
27 | * @inheritdoc |
||
28 | */ |
||
29 | public static function create(ContainerInterface $container) { |
||
35 | |||
36 | /** |
||
37 | * RouteController constructor. |
||
38 | * |
||
39 | * @param \Drupal\graphql\GraphQL\Execution\QueryProcessor $processor |
||
40 | * A GraphQL query processor. |
||
41 | * @param \Drupal\Core\Template\TwigEnvironment $twig |
||
42 | * A Twig environment. |
||
43 | */ |
||
44 | public function __construct(QueryProcessor $processor, TwigEnvironment $twig) { |
||
48 | |||
49 | /** |
||
50 | * Generic page callback. |
||
51 | * |
||
52 | * Accepts a theme hook and an array of theme variables. |
||
53 | * |
||
54 | * @param $_graphql_theme_hook |
||
55 | * The theme hook. |
||
56 | * @param $_graphql_arguments |
||
57 | * Query arguments |
||
58 | * |
||
59 | * @return array |
||
60 | * The render array build. |
||
61 | */ |
||
62 | public function page($_graphql_theme_hook, $_graphql_arguments) { |
||
68 | |||
69 | /** |
||
70 | * Build a page title from a twig template and a GraphQL query. |
||
71 | * |
||
72 | * @param $_graphql_title |
||
73 | * @param $_graphql_title_query |
||
74 | * @param $_graphql_arguments |
||
75 | * |
||
76 | * @return \Drupal\Component\Render\MarkupInterface|\Drupal\Core\StringTranslation\TranslatableMarkup|string |
||
77 | * @throws \Drupal\Component\Plugin\Exception\PluginException |
||
78 | */ |
||
79 | public function title($_graphql_title, $_graphql_title_query, $_graphql_arguments) { |
||
103 | |||
104 | } |
||
105 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.