Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Last active August 20, 2022 18:57
Show Gist options
  • Save jmolivas/d29065493a91f16f35b2 to your computer and use it in GitHub Desktop.
Save jmolivas/d29065493a91f16f35b2 to your computer and use it in GitHub Desktop.

Revisions

  1. jmolivas revised this gist May 8, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions acme.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    .acme-hello-text {
    background-color: #dedede;
    }
  2. jmolivas revised this gist May 7, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion DefaultController.php
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,12 @@ public function hello($name)
    {
    return [
    '#theme' => 'hello_page',
    '#name' => $name
    '#name' => $name,
    '#attached' => [
    'css' => [
    drupal_get_path('module', 'acme') . '/assets/css/acme.css'
    ]
    ]
    ];
    }
    }
  3. jmolivas revised this gist May 7, 2014. 2 changed files with 14 additions and 33 deletions.
    37 changes: 4 additions & 33 deletions DefaultController.php
    Original file line number Diff line number Diff line change
    @@ -3,28 +3,9 @@
    namespace Drupal\acme\Controller;

    use Drupal\Core\Controller\ControllerBase;
    use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Drupal\Core\Template\TwigEnvironment;

    class DefaultController extends ControllerBase implements ContainerInjectionInterface
    class DefaultController extends ControllerBase
    {
    /**
    * @var Drupal\Core\Template\TwigEnvironment
    */
    protected $twig;

    public function __construct(TwigEnvironment $twig)
    {
    $this->twig = $twig;
    }

    public static function create(ContainerInterface $container)
    {
    return new static(
    $container->get('twig')
    );
    }

    /**
    * hello
    @@ -33,19 +14,9 @@ public static function create(ContainerInterface $container)
    */
    public function hello($name)
    {

    $template = $this->twig->loadTemplate(
    drupal_get_path('module', 'acme') . '/templates/hello.html.twig'
    );

    $markup = [
    '#markup' => $template->render([ 'name' => $name ]),
    '#attached' => [ 'css' => [
    drupal_get_path('module', 'acme') . '/assets/css/acme.css'
    ]
    ]
    return [
    '#theme' => 'hello_page',
    '#name' => $name
    ];

    return drupal_render($markup);
    }
    }
    10 changes: 10 additions & 0 deletions acme.module
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <?php

    function acme_theme() {
    $theme['hello_page'] = [
    'variables' => ['name' => NULL],
    'template' => 'hello'
    ];

    return $theme;
    }
  4. jmolivas revised this gist May 6, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion hello.html.twig
    Original file line number Diff line number Diff line change
    @@ -1 +1,3 @@
    <h1>Hello {{ name }}!</h1>
    <div class="acme-hello-text">
    <h1>Hello {{ name }}!</h1>
    </div>
  5. jmolivas revised this gist May 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion DefaultController.php
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ public static function create(ContainerInterface $container)
    public function hello($name)
    {

    $template = $this->twig->loadTemplate(
    $template = $this->twig->loadTemplate(
    drupal_get_path('module', 'acme') . '/templates/hello.html.twig'
    );

  6. jmolivas revised this gist May 6, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions acme.routing.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    acme_hello:
    path: '/acme/hello/{name}'
    defaults:
    _content: '\Drupal\acme\Controller\DefaultController::hello'
    _title: 'acme Title'
    requirements:
    _permission: 'access content'
  7. jmolivas created this gist May 6, 2014.
    51 changes: 51 additions & 0 deletions DefaultController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?php

    namespace Drupal\acme\Controller;

    use Drupal\Core\Controller\ControllerBase;
    use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Drupal\Core\Template\TwigEnvironment;

    class DefaultController extends ControllerBase implements ContainerInjectionInterface
    {
    /**
    * @var Drupal\Core\Template\TwigEnvironment
    */
    protected $twig;

    public function __construct(TwigEnvironment $twig)
    {
    $this->twig = $twig;
    }

    public static function create(ContainerInterface $container)
    {
    return new static(
    $container->get('twig')
    );
    }

    /**
    * hello
    * @param string $name
    * @return string
    */
    public function hello($name)
    {

    $template = $this->twig->loadTemplate(
    drupal_get_path('module', 'acme') . '/templates/hello.html.twig'
    );

    $markup = [
    '#markup' => $template->render([ 'name' => $name ]),
    '#attached' => [ 'css' => [
    drupal_get_path('module', 'acme') . '/assets/css/acme.css'
    ]
    ]
    ];

    return drupal_render($markup);
    }
    }
    1 change: 1 addition & 0 deletions hello.html.twig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <h1>Hello {{ name }}!</h1>