i'm working with Drupal 8, and i'm trying to receive a get variable called id, the url looks like http://localhost/drp/listado/editar/2.
this is the function in the controller
public function edit(Request $request){
$id = $request->query->get('id');
$conexion = \Drupal::database();
$query = $conexion->query("Select * from node where nid = ".$id);
$result = $query->fetchAll();
return array(
'#id' => $id,
'#datos' => $result,
'#theme' => 'editar',
'#titulo' => 'Editar'
);
}
but i get this error in reports/dblogs
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1: Select * from node where nid =;
this is the custom_controller.routing.yml
custom_controller.editar:
path: '/listado/editar/{id}'
defaults:
_controller: '\Drupal\custom_controller\Controller\ListController::edit'
_title: 'Edit'
requirements:
_permission: 'access_content'
it's not getting the value of the variable, any help is welcome, thanks
I think the first parameter your controller is receiving is id, the second parameter is request which is automatic. See here https://www.drupal.org/docs/8/api/routing-system/using-parameters-in-routes
Maybe you can print the contents of the first parameter and die() to check what value Drupal is passing.
Install devel module first and dump $request variable. After hitting url you can get id Parameter.
Related
When a custom attribute is set on a user from googles admin page I am unable to see it in their Google_Service_Directory_User object the client has this scope and is able to see the users data but no attributes are shown.
$client->setScopes(
[
Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY,
Google_Service_Directory::ADMIN_DIRECTORY_GROUP_READONLY,
]
);
I would have expected it to be in the customSchemas but that is null for all the users
When requesting user's informations, you need to set the projection parameter to FULL when using the method users.get (projection parameter is also avaible with method users.list). Default settings to not return custom schemas. You can have more informations here (Google documentation).
You seem to be using PHP (I've never coded in PHP so I may be wrong), so the request should look like this:
$optParams = array(
'userKey' => 'my_customer#example.com',
'projection' => 'full', // or maybe 'FULL' ?
);
$results = $service->users->get($optParams);
If you only want the return some of the schemas, then use :
$optParams = array(
'userKey' => 'my_customer#example.com',
'projection' => 'custom', // or maybe 'CUSTOM' ?,
'customFieldMask' => 'A comma-separated list of schema names'
);
$results = $service->users->get($optParams);
In my config.yml I have this:
parameters:
gitek.centro_por_defecto: 1
Now, I want to change this value from my controller using a form, like this:
public function seleccionAction(Request $request)
{
$entity = new Centro();
$form = $this->createForm(new SeleccionType(), $entity);
$centro = $this->container->getParameter('gitek.centro_por_defecto');
if ($this->getRequest()->getMethod() == 'POST') {
$form->bind($this->getRequest());
if ($form->isValid()) {
$miseleccion = $request->request->get('selecciontype');
$this->container->setParameter('gitek.centro_por_defecto', $miseleccion['nombre']);
// return $this->redirect($this->generateUrl('admin_centro'));
}
}
return $this->render('BackendBundle:Centro:seleccion.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
I´m getting Impossible to call set() on a frozen ParameterBag. error all the time.
Any help or clue?
You can't modify Container once it has been compiled, which is done before invoking the controller.
The DIC parameters are intended for configuration purposes - not a replacement for global variables. In addition it seems you want to persist some kind of permanent modification. In that case consider using session if it's a per-user modification or persisting it (e.g. into DB) if it's supposed to be application-wide.
If you need to modify DIC parameters or services, you can do so using a compiler pass. More info on how to write custom compiler passes can be found at:
http://symfony.com/doc/master/cookbook/service_container/compiler_passes.html
You can set $_ENV variables and get that after
putenv("VAR=1");
And to get
getenv("VAR");
How do you programmatically add a node from a custom form (forms api) and then redirect to that node after saving it?
Figured out the below answer after looking for half a day.
Hope this is useful to someone else!
'type' is the node machine name
'title' is the title you want to give the new node
you can add more 'field_names' that are used in your node
use Drupal\node\Entity\Node;
use Drupal\Core\Url;
public function submitForm(array &$form, FormStateInterface $form_state) {
$newCompanyNode = Node::create([
'type' => 'company',
'title' => $form_state->getValue('company'),
//'field_name' => $value,
]);
$newCompanyNode->save();
drupal_set_message('Your company has been registered.', 'status');
$url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $newCompanyNode->id()]);
return $form_state->setRedirectUrl($url);
}
For more info on how to setup a custom form:
https://www.drupal.org/docs/8/api/form-api/introduction-to-form-api
i am trying to update the value of parameter . I have a hidden field in xml file, i need to update its value , value is dynamic.
I got the parameters using
jimport('joomla.application.module.helper');
$module = JModuleHelper::getModule('mod_name');
I want to avoid the use database query method .
Is there joomla predefined function to achieve this task?
thanks in advance
-Neil
Unfortunately Joomla doesn't have an in-built API feature to set a parameter without using a database query. Try the following:
<?php
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule( 'mod_name'); // change module name here
$params = new JRegistry();
$params->loadString($module->params);
$params->set('param_name', 'value'); // change parameter name and the value
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->update('#__extensions AS a')
->set('a.params = ' . $db->quote((string)$params))
->where('a.element = "mod_name"'); //change module name here
$db->setQuery($query);
$db->query();
?>
I have tested this code so let me know if it works
I set a session var at template preprocess function in a theme I use, but the first time I open the site I cant read the session var, if I refresh the page it works fine, anybody know what can be the problem??
This is the code I use in the preprocess function:
function m_preprocess(&$vars, $hook) {
$default_location = array(
'country_code' => 'ec',
'province_code' => 'p',
'province' => 'Pichincha',
'city' => 'Quito',
'city_path' => 'lugares/u/ec/p/*'
);
if (isset($_COOKIE['proximity_path'])) $default_location['proximity_path'] = $_COOKIE['proximity_path'];
$default_location['path'] = isset($_COOKIE['sort-by']) && $_COOKIE['sort-by']=='proximity'? $_COOKIE['proximity_path'] : $default_location['city_path'];
$_SESSION['location'] = $default_location;
}
A couple of things:
Try dsm($_SESSION); to see what is the var content when the site first load.
I don't know where you create $_COOKIE['proximity_path'], but it is not in the code you show.
Check for typos
The template pre-process function is called before node.tpl.php (that's why it is called pre-process) and that is why the value of the $_SESSION variable is not available in the template preprocess function.
Like you pointed out, it works after the first page load. This happens when only after $_SESSION variable is set in the node body (using the PHP filter)