Is there a way in Drupal 8 to dynamically generate a page for each row in a table in a database?
If I have a database named School, with a table Students, with these columns and rows:
ID |FirstName |LastName |Email |User |HiddenField
-------------------------------------------------------------------------
1 |Adam |Johnson |ajohnson#example.com |ajohn1 |Blah
2 |Bob |Smith |bsmith#example.com |bsmith0 |Foo
Is there a module or setting to create simple pages with these URLs with content like this:
http://mywebpage.com/students/1
First name: Adam
Last name: Johnson
Email address: ajohnson#example.com
Username: ajohn1
http://mywebpage.com/students/2
First name: Bob
Last name: Smith
Email address: bsmith#example.com
Username: bsmith0
Note that there is a HiddenField that isn't displayed, so this solution should include the ability to exclude certain columns.
I've tried using the Views module to create a new page type, but I don't see in there where to specify a table or anything like that.
To fetch data to an associative array with the keys named as the fields, you can write the following:
$query = \Drupal::database()->select('students')
->fields('students', [
'id',
'firstname',
'lastname',
'email',
'user'
]);
$results = $query->execute()->fetchAllAssoc();
After that, you can iterate through $results and create the pages:
foreach ($results as $result) {
// Create node object.
$node = Node::create([
'type' => 'page',
'title' => 'whatever you want',
'field_machine_name' => 'field_value',
'firstname' => $result['firstname'],
]);
$node->save();
}
Please keep in mind to use dependency injection wherever you can. Also you can check for errors like is there a bundle called 'page', and run it only if there is.
Related
using Symfony 5 and Omines, I have 2 tables sending a request to the same url:
the first table show products sold this month,
the second table show products sold before this month
Therefore, I want to pass as a parameter a date. and then in the query_builder filter it.
I do not find any hint in the documentation.
Any help would be great.
Thanks,
add your desired variable which you want to use in querybuilder like shown in example below
$table->createAdapter(ORMAdapter::class, [
'entity' => Product::class,
'query' => function (QueryBuilder $builder) use ($date) {
$builder
->select('p')
->from(Product::class, 'p')
->where("p.date = :date")
->setParameter("date", $date)
;
},
]);
I want to perform bulk update of users with a Approved users, the table
field_user_status_value
-----------------------
entity_type, entity_id, field_user_status_value
The entity_id is the user id which does not exist in the table, below is the custom module I wrote to update the table:
function bulkapprove_action_info() {
return array(
'bulkapprove_action_callback_name' => array(
'type' => 'user', // Can be file, term, user, etc.
'label' => t('Approve User'),
'configurable' => FALSE, // Doesn't need config form
'behavior' => array('view_property'), // Uses view access rights ,
'pass rows' => TRUE,
'triggers' => array('any'), // Works always
),
);
}
function bulkapprove_action_callback_name($entity, $context)
{
db_update('field_data_field_user_status')->fields(array('field_user_status_value' => 'Approved'))->condition('entity_id', $context->entity_id)->execute();
}
But it is not inserting the values in this table
In Drupal you do not want to update the database fields directly unless you created the table. Drupal's internal APIs provide a collection of tools to ensure you update the values correctly and that all supporting modules get notified of changes as needed through the hook system.
In this case the callback gets the actual entity to run your action against (in this case the user object). You want to take action on that entity and then save the entity.
function bulkapprove_action_callback_name($entity, $context)
{
$entity->status = 1;
entity_save('user', $entity);
}
I have custom table with a field called image_id.
image_id is an integer which represents a file id of the managed_file table.
I've read many tutorials on how to expose this field to views, and how to make a join with the managed file table. All examples just show joins with node table, but none of them make joins with file_managed.
I tried something like this:
$data['my_table']['table']['join'] = [
'file_managed' => [
'left_field' => 'fid',
'field' => 'image_id',
],
];
$data['my_table']['image_id']['relationship'] = [
'handler' => 'views_handler_relationship',
'base'=>'file_managed',
'field'=>'image_id',
'label' => 'Managed files',
];
Right now my field is exposed, but the relation with the file_managed table doesn't work. I can't see fields from the managed files table, therefore I can't render my images with all formatters available.
Please help me!
I want to pass a parameter value to the route with the content of the current cell.
$listMapper->add("parent_id", 'url', array(
'route' => array(
'name'=>'admin_xxxxx_news_news_show',
'absolute'=>true,
'identifier_parameter_name'=>"parentID"
)
);
I have a route admin_xxxxx_news_news_show and have to pass the parameter from the current column. I don't have a relation of the current field with the related entity. I am using two columns for association, one for the entity and second for that entity ID. With my current knowledge of Symfony2 I need to have a customized view.
I have built a custom registration form using module_form_alter hook. I have also added the required new fields to the database with db_add_field. Now I'm able to add the values to the table in user registration/ user profile edit and the values are also getting stored in the database.. But what I'm not able to do is get the values that are stored in the database in the user profile edit form is displayed. Is there a hook to load the values from database to form on form load? Or is there any other way?
function customUser_schema_alter(&$schema) {
// Add field to existing schema.
$schema['users']['fields']['detail'] = array(
'type' => 'varchar',
'length' => 100,
);
}
function customUser_install() {
$schema = drupal_get_schema('users');
db_add_field('users', 'detail', $schema['fields']['detail']);
}
function customUser_form_alter(&$form, &$form_state, $form_id) {
// check to see if the form is the user registration or user profile form
// if not then return and don’t do anything
if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
return;
}
$form['account']['detail'] = array(
'#type' => 'textfield',
'#title' => t('Additional Detail'),
);
}
A proper answer needs more details. I can only assume what you did.
You added fields to the {users} table. You didn't update the database schema which made drupal_write_record not be aware of the new fields, that being the reason they are not populated.
You created a new table {my_table} with the fields.
In both cases you need hook_user_insert()
/**
* Implements hook_user_insert().
*/
function mymodule_user_insert(&$edit, $account, $category) {
// Here you add the code to update the entry in {users} table,
// or int your custom table.
// $edit has the values from the form, $account->uid has the
// uid of the newly created user.
}
Note: If my first assumption is true that's not the drupal way to do it. You should have done the 2nd way instead. And even in that case use the hook_schema to create your table in mymodule.install instead of doing db_add_field().
For drupal 7 you could have uses the profile module (core) or profile2 to achieve that.
Based on that code
Try to change to this inside the form alter.
$account = $form['#user'];
$form['account']['detail'] = array(
'#type' => 'textfield',
'#title' => t('Additional Detail'),
'#default_value' => $account->detail,
);