Skip to content
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

VOTE-2114 Send variable to twig template from controller (example) #947

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions web/modules/custom/vote_nvrf/src/Controller/NvrfPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ public function content(string $state_name) {
'vote_nvrf/nvrf_assets',
],
],
//'#state_name' => $title,
//'#theme' => 'page__node__vote_nvrf_nvrf_page',
];
}

return $build;
}

//Test function - Send varibales to template
public function getStateName(string $state_name) {
// Transform the state name string
$title = ucfirst(str_replace('-', ' ', $state_name));

return [
'#state_name' => $title,
'#theme' => 'pass_state_name_test'//'pass_state_name_test'
];
}
}
13 changes: 13 additions & 0 deletions web/modules/custom/vote_nvrf/vote_nvrf.module
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,16 @@ function vote_nvrf_theme_suggestions_page_alter(array &$suggestions, array $vari
$suggestions[] = "page__node__" . str_replace('.', '_', $current_route_name);
}
}

/**
* Implements hook_theme().
*/
function vote_nvrf_theme($existing, $type, $theme, $path){
return [
'pass_state_name_test' => [
'variables' => [
'state_name' => NULL,
],
],
];
}
8 changes: 8 additions & 0 deletions web/modules/custom/vote_nvrf/vote_nvrf.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ vote_nvrf.nvrf_page:
requirements:
_permission: 'access content'
state_name: '[a-z-]+'

vote_nvrf.test_state_name:
path: '/register/{state_name}/form/test'
defaults:
_controller: '\Drupal\vote_nvrf\Controller\NvrfPageController::getStateName'
requirements:
_permission: 'access content'
state_name: '[a-z-]+'
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</header>

{{ page.admin }}

<h1>{{ state_name }}</h1>
{% block main %}
<main role="main" id="main" class="grid-container">
{{ page.content }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>State Name: {{state_name}}</h1>