Drupal 8 Configuration Management Issues


Everyone seems very excited with new configuration management tools provided by drupal 8. It just packages all (or single) site settings and you can import it to your other environments. But there are few glitches those can give developers hard time. And I am one of those developer. Here are few issues, I noticed while working with other developers and using configuration management.

1. Unable to install the standard module since it does not exist.

First of all, I had no idea what’s going on here. Later I figured out that, one of the developer installed fresh drupal  with standard profile and later changed the site hash to match the old installation. Now as configuration is looking for standard extension which does not exist on our site and configuration import fails with message ‘unable to install the standard….’. There are two ways to fix this issue:

  1. Make backup of you configuration files. Copy to your imported configuration files to your config folder and edit ‘core.extension.yml’. Remove the line standard: xxxx under module. Now, you can sync the config.
  2. Edit settings.php file, and paste:
$settings['install_profile']  = 'standard';

I would recommend to try first method and second if first does not work.

2. Configuration source control issue

When you install drupal, you will get weird directories. If you want to have common folder for configuration sync, you can define the directory where you want to have all config files. I personally prefer to use ‘sites/xxxx/sync’ folder. To use it as sync folder, you need to make add line of setting in settings.php.

$config_directories['active'] = 'sites/xxxx/sync'; //xxxx is your site folder, default in most of the cases

3. Configuration Management vs features

As you have noticed, features module is still there even drupal 8 is armed with configuration management. Features team has their own explanation on why you still need feature. As a rule of thumb, if you want to package certain collection of feature which you can use in other sites easily, you need features module. For example, If you want to use all articles related configurations in different site, you need feature in this case. You can use configuration management too but that will too much work. Using feature you can easily package them and import them in new site. You can learn details of configuration management vs features. https://www.youtube.com/watch?v=a6QLTjSFVhs and http://www.slideshare.net/nuvoleweb/2015-04devdaysmontpellier

 

 

Advertisements

Drupal: Use custom template file for any form


Introduction

Drupal 7 has well known template file concept. Template (.tpl) file is used to display the data and has minimal or no logic at all. I am pretty sure there are many scenarios where you would like to use your own template file. Most of the cases you can easily create template file using well documented instructions. For example, if you want to create template file for content type ‘Article’, you simply create a template file ‘node–article.tpl.php’ and put it inside your theme’s template folder. Drupal will automatically detects that template file and use it instead of default node.tpl.php. Similarly you can see theme suggestion of views just clicking on advanced->theme. But there are certain scenario where there is straight way to create and utilize template file. For example, you may want to customize login screen of your site and want to change layout or add more classes or add new div wrapper. To make use of template file in such scenario, we need to implement certain hooks.

Steps

First you need to add YOURTHEME_theme hook function in you theme->template.php file.

/**
 * theme_theme hook
 * @return type
 */
function MYTHEME_theme() {
    return array(
        'user_login' => array(
            'render element' => 'form',
            'path' => DIRECTORY_PATH_WHERE_YOU_WANT_TEMPLATE_FILE,
            'template' => 'user-login',
            'preprocess functions' => array('MY_FORM_PREPROCESS'),
        ),
    );
}

Now, if you want to update form data or add your own logic, you can use your preprocess function. Moreover, you can define your own custom variables which will be available to use in your template file. In the example below, I am creating new variable ‘myvariable’ which I can use in my template file later.

function MY_FORM_PREPROCESS(&$vars) {
    // Here you can have your own logic and 
    // you can define your own variables 
    // which will be available for you in your tpl file
    $vars['myvariable']='COMPLEX ARRAY';
}

Final step is to create tpl file in the directory you specified earlier. In this example, we have template name ‘user-login’. So, file name will be ‘user-login.tpl.php’. If you forget creating tpl file or misname it, drupal will fall back to use default way.

Further Question

Please leave your comment/question, if you have any concern implementing template file this way.

PHP and Accessing MSSQL Server in Unix/Linux


Introduction

Microsoft provides a very good driver and documentation on how to connect and work with their MS SQL Server. Unfortunately, it only works on windows operating system. So for Linux/Unix, you need to find different approach to connect with. FreeTDS is there for you to rescue. It is free and works with both Linux and Unix.

Details

For this post, I am using Ubuntu 12. The first step is of course having proper working FreeTDS. So, for this you need to install several packages. In your terminal just execute the command:


apt-get install freetds-bin freetds-common tdsodbc odbcinst php5-odbc unixodbc

This command will install all necessary components to add ability to connect to remote MSSQL server.

Second step is to copy the odbcinst.ini file from /usr/share/tdsodbc (in some case /usr/share/doc/freetds-common/examples/odbcinst.ini) to /etc. Yes, you can use GUI to copy the file but just make sure you backup the existing odbcinst.ini file in /etc. If you want to do this by command line here you go:


mv /etc/odbcinst.ini /etc/odbcinst.ini.bak

cp /usr/share/tdsodbc/odbcinst.ini /etc/

Yes!!! You are done with the configuration step. Now it is the time of coding, real challenge.

As you have your TDS driver and odbc in your unix, you can create PDO connection object and execute query based on that connection. Here you go:

<?php

 try {

  //$con = new PDO('odbc:Driver=FreeTDS; Server=full_machinename_or_ip_address\(servername_if_any); Database=db_to_connect; UID=username;PWD=password;);
  $con = new PDO('odbc:Driver=FreeTDS; Server=remote_server\mssqlserver; Database=db_students; UID=db_user; PWD=password123;');
  // Now you have connection object. You can query on your db.
  // Example to execute ful SQL query
  $result = $con->query('SELECT Name FROM dbo.students');
  foreach ($result as $row) {
   print $row['Name'] . '<br />';
  }

  // Example to execute stored procedure
  $result = $con->query('EXEC dbo.GetAllStudents');
  foreach ($result as $row) {
   print 'Name: ' . $row['Name'] . ' Grade: ' + $row['Grade'] . '<br />';
  }
  $con = null;
 } catch (PDOException $e) {
  echo $e->getMessage();
 }

Yes for Remote local table above code will work fine but the above code will fail when there is Linked Server. This means MSSQL Server has linked database which is somewhere else and can be other database server like oracle. In this case your $result will be false. To ensure that your code doesn’t fail on such type of scenarios, you need to execute two commands before actual query. Here is the full code which will work for Linked Server.

<?php

try {

 $con = new PDO('odbc:Driver=FreeTDS; Server=remote_server\mssqlserver; Database=db_students; UID=db_user; PWD=password123;');

 // Make sure it won't fail for Linked Server
 $command = $con->prepare('SET ANSI_WARNINGS ON');
 $command->execute();
 $command = $con->prepare('SET ANSI_NULLS ON');
 $command->execute();

 // Now you have connection object. You can query on your db.
 // Example to execute ful SQL query
 $result = $con->query('SELECT Name FROM dbo.students');
 foreach ($result as $row) {
  print $row['Name'] . '<br />';
 }

 // Example to execute stored procedure
 $result = $con->query('EXEC dbo.GetAllStudents');
 foreach ($result as $row) {
  print 'Name: ' . $row['Name'] . ' Grade: ' + $row['Grade'] . '<br />';
 }

 $con = null;
} catch (PDOException $e) {
 echo $e->getMessage();
}
?>

Yes you are done now. You can connect to MSSQL Sever from non windows machine and from PHP. If you have any questions just let me know via comments.