15

I need to attach a field to some bundles. A PHP snippet will do the job for me, so I don't need a module.The below snippet is what must be executed :

$field_name = 'field_custom_image';
// Create the field.
$field = array(
  'field_name' => $field_name,
  'type' => 'image',
);
field_create_field($field);

and then the snippet to add this field to bundle :

// Create the instance.
$instance = array(
  'field_name' => $field_name,
  'entity_type' => 'node',
  'bundle' => 'article',
  'label' => 'Image',
  'description' => 'An image for the article.',
  'required' => TRUE,
);
field_create_instance($instance);

Is there a way to run PHP code directly in Drupal, without the need to write a module, install it, make it run, and uninstall it?

  • Thanks everyone for both working answers and good editions made to my question. I voted all answers up as solutions to my problem,now it is a hard decision for me to select one of those answers as the best solution cause all works. – Alireza Tabatabaeian Sep 7 '15 at 12:00
  • 1
    If you were to show the code you need to run, the answers would be more complete. – kiamlaluno Sep 7 '15 at 12:09
  • @kiamlaluno Thanks for advice, I will attach the PHP snippet to my question. – Alireza Tabatabaeian Sep 7 '15 at 12:12
  • 1
    Closely related question & answers: How to execute php script using drush? – tanius Sep 7 '15 at 15:19
6

If you're saying "I need to run some PHP" and "I don't need a module" in the same thought, I'd suggest that you're going down the wrong path, either in the approach to this specific problem, or the approach to building the site in general.

drush scr will help you if that's the direction that you really want to go, or you could write a standalone PHP script (basically just copy/paste everything through the drupal_bootstrap() call from index.php, and add your custom code after) but there are a couple other things you could look at:

  • Export your content type (and it's field bases and instances) with the Features module. IMO, you should be doing this anyway.
  • Create a simple "site modifications" module, and include things like this in update hooks. Note that if you want to do this, you should create an empty module (with no update hooks), enable it on your site, and then add your update hook to get Drupal to see the update.

Many sites that I've worked on have had a combination of both of those solutions. Config should mostly go into exported Features modules, and anything that can't (are you really sure it can't?) can go into an update hook.

  • Thanks for advises , specially for informing about the use of update hooks . very usefull – Alireza Tabatabaeian Sep 9 '15 at 0:41
  • Could you please mention some problems which may be caused by running PHP codes this approach? I mean attaching fields to some content types just by running PHP scrips? – Alireza Tabatabaeian Oct 24 '15 at 15:10
18

Yes, there is, but given your description I wonder if it will help you:

  1. Install module Devel
  2. Go to http://yoursite/devel/php
  3. Enter your code and execute it.
  • 1
    The Devel module also comes with an "Execute PHP" block, that you can place in a region. – No Sssweat Oct 30 '17 at 23:05
12

If you need to run code just once, you can do it without installing any module, including the Devel module.

Write a PHP file (e.g. tasks.php), put it where the cron.php file that comes with Drupal is, and add the following code.

/**
 * Root directory of Drupal installation.
 */
define('DRUPAL_ROOT', getcwd());

include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
drupal_set_time_limit(240);

// Your code here.

If you are already using Drush for maintaining/deploying your site, then you could make a Drush script. In this way, it could be executed by CLI, and it could receive any arguments, making it re-usable for many occasions.

#!/usr/bin/env drush

// Your code here.
// Access the options with drush_get_option(), or any argument with drush_shift().

You could also execute a PHP script with drush php-eval (or just drush ev), for example:

drush php-eval 'print time();'

I would create a Drush script, but if you cannot install Drush, or you need to execute PHP code from the browser, the first method works fine.

10

Add your code snippet to a file and save the file as file_name.php in your Drupal site root folder. Then run the following command in a terminal, from within the Drupal site root folder as your working directory:

drush scr file_name.php

This command will execute your PHP file. (To get an idea of the execution, you may include some print statements in your file, which will output the text, variable values etc. to the terminal where you execute the drush command.)

Note: As a prerequisite, you need to have drush installed on your system.

3
+50

You can use Php inline of Devel module but lets get you one of my tricks in drupal to running my custom code without any module.

first create a menu item in hook_menu

function yourmdoule_menu()
{

    $items['ytest'] = array(
        'title' => 'my debug and devel test tool ',
        'description' => 'create for debug and faster develop',
        'page callback' => 'my_function',
        'type' => MENU_CALLBACK,
        'access callback' => true,
    );
    return $items;
}

function my_function{

/// run every want you want
}

then clear the cache ( because menu items cached in drupal ), and put everything you want in my_function and just type yoursite.com/ytest

I hope it will help you in projects "dada" ;)

  • @AlirezaTabatabaeian you're welcome "dada", I'll be glad when see Iranian Drupalist here, ;) – Yusef Mohamadi Sep 7 '15 at 12:18
2

The goal of Drupal modules is to provide separate logic to certain components, so they can be disabled if something goes wrong.

hook_install/hook_update

In your case which is creating a new field on one-time basis, the ideal scenario is to use either hook_install or hook_update.

If you don't have a custom module (btw. you can also use exported feature module for that), create a new one just for updates (e.g. site_updates). In Drupal 7, all you need is to create folder with 3 files: .info, .module and .install and define install or update hook in your .install. Using install/update hook is convenient in order to have consistent environments, especially when you've multiple Drupal servers (such as dev, test and prod), so the change can be invoked during the next deployment.

If you still don't want to create module, here are other methods.

One-time execution

If you need only to execute certain PHP code only one time, you can:

  • Activate Devel module and paste the code at /devel/php,
  • Use drush CLI (and its eval/ev command) to execute PHP code, e.g.

    drush ev 'field_create_field(["field_name"=>"foo", "type"=>"image"]);'
    drush ev 'print_r(field_info_field("foo"));'
    
  • Use drush scr to execute PHP script:

    drush scr my_file.php
    

    where my_file.php is your plain PHP script with your code to execute.

Specific page execution

To execute custom code on certain pages or components:

All page execution

To add and run PHP code on every page without writing a module, you can:

  • Use a settings.php file for that. It is only recommended for simple code such as configuration override. Otherwise you should use hook_init for that as part of your custom module.
  • Activate PHP text filter, then create a block with custom PHP code in it and place the block into footer region.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.