You are here

drupal

Bootstrapping Drupal 7 and adding a new admin account

Recently I had FTP access to a site, but had lost my Drupal user/pass. I was able to create a PHP file that I uploaded to the root of the Drupal installation that bootstrapped Drupal and created a user with administrator access. The code used is found below…

The code is also a good example of how to bootstrap Drupal, albeit from the Drupal installation root folder. If you want to bootstrap Drupal from another folder, make sure to change DRUPAL_ROOT to the Drupal installation folder, and make sure the "include_once(…" line points to the correct file.

<?php // Grants 'administrator' rights to a new username define('USERNAME', 'username'); define('PASSWORD', 'password'); define('EMAIL', 'email address'); // Set the header to something that produces readable output in the browser header('Content-type: text/plain'); // Bootstrap Drupal define('DRUPAL_ROOT', '.'); include_once(dirname(__FILE__) . '/includes/bootstrap.inc'); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // Get an array of defined user roles echo "Defined roles:\n"; $roles = user_roles(); var_dump($roles); // Find the Role ID for the 'administrator' role $adminRole = 'administrator'; $adminRID = array_search($adminRole, $roles); echo "\nAdministrator role ID: $adminRID\n"; // Try to load the user account with the specified username echo "\nExisting account: "; $account = user_load_by_name(USERNAME); echo $account === FALSE ? 'does not exist' : 'found'; // Update the user account, setting it to active and granting it the role 'administrator' echo "\nSetting account to active and assigning new role:\n"; $newRole = array(); $newRole[$adminRID] = $adminRole; $account = user_save($account, array( 'name' =?> USERNAME, 'pass' =&gt; PASSWORD, 'mail' =&gt; EMAIL, 'init' =&gt; EMAIL, 'status' =&gt; 1, 'roles' =&gt; $newRole, )); var_dump($account);

Adding a custom link to a Drupal 7 Views Template

Anyone familiar with Drupal knows Views. It's great for pulling information from the database and displaying it in just about any format you want. I recently set up a job listing site, which was pretty trivial task in Drupal (I've seen guides on setting up job listing sites in a few Drupal books). The problem I had was when I needed to add a separate "read more" link to the job listing after the description text.

Image of the job listing site

Overriding the default output on the table was simple… Views shows the list of suggested template override filenames under "Advanced → Other → Theme: Information". In my case, the template to create "theme_directory/templates/views-view-field--list-all-jobs--page--body.tpl.php". I started by copying the content of "/sites/all/modules/views/theme/views-view-field.tpl.php" and modifying to taste. Creating the "read more" link to that row's node was done using the following code.

print $output; echo '<a href="', url('node/'. $row-&gt;nid, array('absolute' =&gt; TRUE)), '" class="readMore">Read more…</a>';

The documentation for the function url(...) can be useful in understanding what's happening here. Basically, we're using the node ID to make the URL "node/X", and then asking the "url" function what the absolute URL for that path should be. This returns the full path for that link as defined in the alias for the node.

Subscribe to RSS - drupal