Permalink
Browse files

Added simple page title field.

  • Loading branch information...
pmelab committed Aug 9, 2017
1 parent e7976c0 commit c06d828612ce2f36a90d137e8ac39a2b416c2ad8
Showing with 39 additions and 0 deletions.
  1. +39 −0 src/Plugin/GraphQL/Fields/PageTitle.php
@@ -0,0 +1,39 @@
<?php
namespace Drupal\graphql_example\Plugin\GraphQL\Fields;
use Drupal\graphql_core\GraphQL\FieldPluginBase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Youshido\GraphQL\Execution\ResolveInfo;
/**
* A simple field that returns the page title.
*
* For simplicity reasons, this example does not utilize dependency injection.
*
* @GraphQLField(
* id = "page_title",
* type = "String",
* name = "pageTitle",
* nullable = true,
* multi = false
* )
*/
class PageTitle extends FieldPluginBase {
/**
* {@inheritdoc}
*/
protected function resolveValues($value, array $args, ResolveInfo $info) {
// Get the current Druapl request.
$request = \Drupal::request();
// Get the request route.
if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
// If there is a route, yield the generated title.
yield \Drupal::service('title_resolver')->getTitle($request, $route);
}
}
}

0 comments on commit c06d828

Please sign in to comment.