Skip to main content

Posts

PHP: Memory consumption in different scenarios by counting memory used by PHP (but not allocated to PHP)

There is a question which got me sleepless last night. I was sure about the results but had no real tests. The tests I made today may not be the best but they definitely allowed me to verify my thinking. I was actually trying to find out if the code residing in a PHP function made a difference to memory usage even if the function is not called anywhere.

Long story short, if you want to save some memory from functions which should be globally available but not executed all the time, you may add their code in a different function in a file-to-include when the function is called.

The scenarios' PHP files are found at the following link:
https://gist.github.com/vensires/d2d34b1bbdec99e7c522333d7c620070
Recent posts

hook_field_extra_fields() are enabled by default on all content types

So what? Just use the following code in your hook_install() function to disable it in every content type and in every view mode available in your website. Or if you are working on a custom module and you only want to execute this once, use Devel's "devel/php" page and paste it there!
$field_name = 'THE_MACHINE_NAME_OF_YOUR_CUSTON_FIELD';
$types = array_keys(node_type_get_types());
foreach($types as $bundle) {
  $entity_type = 'node';
  $field_bundle_settings = variable_get("field_bundle_settings_{$entity_type}__{$bundle}", array());
  $view_modes = field_view_mode_settings($entity_type, $bundle);
  foreach($view_modes as $view_mode => $data) {
    if($data['custom_settings'] == TRUE) {
      $field_bundle_settings['extra_fields']['display'][$field_name][$view_mode]['visible'] = FALSE;
    }
  }
  variable_set("field_bundle_settings_{$entity_type}__{$bundle}", $field_bundle_settings);
}

Change language to send an email

It isn't something I do in my everyday life... In fact, it's the very first time I was required to implement this.Scenario:The administrator of a Drupal Commerce shop has to be able to send personalized emails to his clients. The email has to be personalized and the language of the email will have to change accordingly.To facilitate the process for the administrator, I decided to create multilingual variables for some parts of the email and allow the administrator to change them from the UI with a common Drupal settings form.The Problem:Using variables is administrator-friendly. But when sending an email in English when the page is loaded in Greek means that any variable_get() calls will load the Greek text, even if the variable module is enabled. Variables are actually stored in memory after variable_initialize() has been called. As a result, you may change the global $language object to the language of your choice before printing any texts not wrapped in t(), but the variabl…

How to use rsync to sync remote and local filesystems or servers

Trying to transfer a website with a lot of files from an old hacked server to a new one I used the following command. I also tried to use the sftp protocol. I also tried to transfer the compressed file to a public folder and download it from there but Apache didn't seem to handle it properly. rsync was and will be the command of my choice for the next years to come it seems. PS: Make sure you also check the related article on DigitalOcean.

Override the default Opigno LMS navigation menu

I recently used Opigno LMS for a client's project. It's actually a LMS distribution of Drupal. It supports WebEx meetings, Lessons, even PDF Certificates after you complete a Course. In my case though I needed Lessons but no PDF Certificates. Disabling the proper apps/modules wasn't anything out of the ordinary Drupal procedure. What was lacking though was a way to override the main menu to not display the My Achievements item.My Achievements is enabled as long as the Opigno Quiz App (@see Lessons above) is enabled and no hook exists to override this functionality. To disable it I had to create a custom theme using the Platon theme as a base (Platon is the default theme of the Opigno LMS) and copy 3 functions of Platon's template.php file to my theme's template.php with the proper modifications to get what I wanted.See the code below...