Dealing with core Cache in Drupal 8

At this point, if you try to login in the Drupal 8 website you will be rejected, it’s because the login system doesn’t read directly the table users_field_data instead of a cache for entities is used.

To flush the cache for a specific user entity with compromise the rest of cache of your system you can use the following SQL statement.

DELETE FROM cache_entity WHERE cid = ‘values:user:1’;

Now you can grab a cup of coffee/tea and enjoy your Drupal 8 website.

Hope this will help you.

 

Advertisements

Resetting the administrator password with SQL-query in Drupal 8

It’s has been happening, that we tend to forget password on local as well for the live site. When it’s come to Drupal 8 all things has things changed from the prior version of Drupal.

Here we go with some of the tricks for Drupal 8.

The Solution
Generate a new password
First, you have to generate a password hash that is valid for your site.

Execute the following commands from the command line, in the Drupal 8 root directory:

$ php core/scripts/password-hash.sh ‘your-new-pass-here’

password: your-new-pass-here    hash: $S$EV4QAYSIc9XNZD9GMNDwMpMJXPJzz1J2dkSH6KIGiAVXvREBy.9E

Update the user password.

Now you need to update the user password, in our case, we need to update the Administrator password, fortunately, the UID for Administrator is 1 equal to previous versions of Drupal.

With the new password, we need run the following SQL statement.

UPDATE users_field_data SET pass=’$S$E5j59pCS9kjQ8P/M1aUCKuF4UUIp.dXjrHyvnE4PerAVJ93bIu4U’ WHERE uid = 1;

We all set with password update and go… log in!

Page Rendering and Altering in Drupal

Step 1: The Router Item
After Drupal has been bootstrapped, the last thing index.php does is call
menu_execute_active_handler(). This retrieves the router item from the menu_router based on the
current path. There is an important chance here to intervene, hook_menu_item_alter()

Step 2: The Page Callback Is Fired
For example, the default home page returns this:
$build[‘default_message’] = array(
  ‘#markup’ => “<p>No front page content has been created yet.</p>”,
  ‘#prefix’ => ‘ ‘,
  ‘#suffix’ => ‘ ‘,
);

Step 3: The Delivery Callback

Now you are back in menu_execute_active_handler(). The next callback to fire is the delivery callback,
which defaults to drupal_deliver_html_page(). As the name suggests, by default you are delivering the
page callback results in HTML.

function overlay_deliver_empty_page() {
  $empty_page = ‘<html><head><title></title>’ . drupal_get_css() . drupal_get_js() .
  ‘</head><body class=”overlay”></body></html>’;
  print $empty_page;
  drupal_exit();
}

Step 4: drupal_render_page()

First, hook_page_build() is called, which allows other modules to add to the page array—because
here you build that. Your modules can add to the page array, also. Second, hook_page_alter() is fired.

Step 5. hook_page_alter()

Perform alterations before a page is rendered.

Use this hook when you want to remove or alter elements at the page level, or add elements at the page level that depend on an other module’s elements (this hook runs after hook_page_build().
For Example Page rendar hook :

function dgd7_page_alter(&$page) {
 if ($node = menu_get_object()) {
  $comments = &$page[‘content’][‘system_main’][‘nodes’][$node->nid][‘comments’][‘comments’];
  $comments[‘ad’] = dgd7_get_ad();
  // The first comment weight is 0, the second is 1, go between them.
  unset($comments[‘#sorted’]);
  $comments[‘ad’][‘#weight’] = 0.5;
 }
}
function dgd7_get_ad() {
  return array(‘#markup’ => t(‘Hello I am an ad!’));
}

Step 6. drupal_render()
This is a recursive function called on every child of the page array. So you start with running
drupal_render($page), then continue with drupal_render($page[‘content’]), then with
drupal_render($page[‘content’][‘system_main’]), and finally with
drupal_render($page[‘content’][‘system_main’][‘default_message’]).

So for example:
$element[‘#cache’] = array(
  ‘cid’ => ‘foo:bar’,
  ‘bin’ => ‘cache_something’,
  ‘expire” => 900,
);

Vim Plugin for Drupal on Linux

Indentation

The following commands will indent your code the right amount, using spaces rather than tabs and automatically indent after you start. The commands should be added to a .vimrc file in your home directory (~/.vimrc), you may need to create this.
set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent

Syntax highlighting

If you enjoy syntax highlighting, it may be worth remembering that many of Drupal’s PHP files are *.module or *.inc, among others.

Vim seems to syntax highlight *.inc files properly by default but doesn’t know that some other files are PHP content. For *.module and *.install, use this snippet in .vimrc:
if has(“autocmd”)
” Drupal *.module and *.install files.
augroup module
autocmd BufRead,BufNewFile *.module set filetype=php
autocmd BufRead,BufNewFile *.install set filetype=php
autocmd BufRead,BufNewFile *.test set filetype=php
autocmd BufRead,BufNewFile *.inc set filetype=php
autocmd BufRead,BufNewFile *.profile set filetype=php
autocmd BufRead,BufNewFile *.view set filetype=php
augroup END
endif
syntax on

more can be done its all open source.. it’s upto you 🙂

Programmatically Adding a Field Collection Item to a exiting Node in Drupal

In such case where you need to add field item to a node like text,link field's in a node for drupal 7


function my_custom_module_field_collection($nid, $fid){
   // Load target node
   $node = node_load($nid);

   // Create a new field collection 
   $fieldCollectionItem = entity_create('field_collection_item', array('field_name' => 'my_field_collection_item_name'));

   // Prepare link field
   $link = array(
      'title' => "",
      'url' => "",
      'attributes' => array(
         'title' => "",
      ),
   );

   // Prepare file 
   $file = (array)file_load($fid);
   $file['display'] = "1";

   // Load items into field collection
$fieldCollectionItem->field_image[LANGUAGE_NONE][] = $file;

$fieldCollectionItem->field_link[LANGUAGE_NONE][] = $link; // Save field collection item

$fieldCollectionItem->setHostEntity('node', $node);

$fieldCollectionItem->save(TRUE); node_save($node);
 
}

 

Disable Outgoing Mail in Drupal 7

/**
* Implements hook_mail_alter
*/
function yourmodule_mail_alter(&$message) {
// set ‘To’ field to nothing, so Drupal won’t have any address
$message[‘to’] = ‘ ‘;
}

Other Way you can do

If using devel with drupal 6, add to settings.php
$conf[‘smtp_library’] = ‘sites/all/modules/devel/devel.module’;

and all outgoing emails get logged in watchdog.

If using devel with in drupal 7, if you add
$conf[‘mail_system’] = array(‘default-system’ => ‘DevelMailLog’);

emails get written to files in the drupal temporary directory.


Re-route All Emails Using Reroute Email Module

Download and install the Reroute Email module.

Go to Configuration-> Reroute Email (admin/config/development/reroute_email).

And  than Check the Enable rerouting checkbox and enter in an email address into the Email addresses text field.

 

 

 

Install Drupal using Drush command only

For setup the empty drupal project setup use following command

#drush dl drupal-7
Project drupal (7.28) downloaded to /home/manish/drupal-7.28.                                                                                                       [success]
Project drupal contains:                                                                                                                                           [success]
– 3 profiles: testing, minimal, standard
– 4 themes: stark, seven, bartik, garland
– 47 modules: drupal_system_listing_incompatible_test, drupal_system_listing_compatible_test, poll, rdf, statistics, user, taxonomy, translation, tracker, node,
contact, syslog, contextual, profile, overlay, forum, blog, book, system, aggregator, path, comment, help, simpletest, trigger, menu, dashboard, search, locale,
field_ui, shortcut, options, number, field_sql_storage, text, list, field, php, color, toolbar, filter, update, dblog, file, block, image, openid

#cd drupal-7.28

#drush site-install standard –account-name=admin –account-pass=admin –db-url=mysql://mydbusername:’pass@123’@localhost/mydbname

You are about to create a sites/default/files directory and create a sites/default/settings.php file and DROP all tables in your ‘marketplace’ database. Do you want to continue? (y/n): y
Warning: Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1
Starting Drupal installation. This takes a few seconds …                                                                                                         [ok]
Installation complete.  User name: admin  User password: admin

after this you can check project in browser.

http://localhost/drupal-7.28