Is there any way to execute Drupal 8 functions from an external PHP file.
You can include/call Drupal's bootstrap in your script and after that call Drupal's functions. Used that for D7, but didn't try for D8. However it should work:
https://drupal.stackexchange.com/questions/174474/bootstrap-from-external-script
And to copy code from that page:
define('DRUPAL_DIR', '/usr/share/nginx/html');
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
require_once DRUPAL_DIR . '/core/includes/database.inc';
require_once DRUPAL_DIR . '/core/includes/schema.inc';
// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
$kernel->prepareLegacyRequest($request);
$em = $kernel->getContainer()->get('entity.manager');
$entity = $em->getStorage('node')->create(
array(
'type' => "article",
'title'=> "test entity",
'body' => "body body body",
));
$entity->save();
If you have Drush, you can run "drush scr [filename]" which will execute the file as well as bootstrap Drupal. I did a blog post about this - https://www.oliverdavi.es/blog/dont-bootstrap-drupal-use-drush.
I'd only use this for simple local test scripts though to test things out before moving the code into proper functions/classes/controllers/services etc.
You can run any php code either drupal or non-drupal using Devel and Kint module like this.[Use Devel and Kint module] like this
https://i.stack.imgur.com/RUKv6.png
Related
i thought i would reach out to get some guidance on a little thing i am working on.
What i would like to do within Wordpress:
Call external API (with token header)
Get the results of the api and save it into a file in wpallimport's upload folder
I would assume i can just make a simple WP plugin and within the 'activate' hook for the plugin:
create a wp-cron (as i would like it to run every day) for the following:
$url = 'the-api-url';
$data = wp_remote_get( $url ,
array('headers' => array( 'Token' => 'tokenkey')
));
$jsonfile = $data['body'];
global $wp_filesystem;
if (empty($wp_filesystem)) {
require_once (ABSPATH . '/wp-admin/includes/file.php');
WP_Filesystem();
}
$file = '/wp-content/uploads/wpallimport/files/JSONFILE.JSON';
$wp_filesystem->put_contents($file, $jsonfile);
However i am not having success with the above (with the correct API url and token etc obviously)
Thanks in advance!
i am using a single php-file Drupal bootstrap to generate a standalone cronjob PHP file.
Here is my setup (the file resides in a subfolder, thats why i have to build up the base path like this)
/**
* Drupal bootstrap
*/
$path = str_replace("sites/all/modules/custom/my_module/cron",
"", DIRNAME(__FILE__));
define('DRUPAL_ROOT', $path);
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
//require_once DRUPAL_ROOT . '/includes/common.inc';
//require_once DRUPAL_ROOT . '/includes/module.inc';
//require_once DRUPAL_ROOT . '/includes/unicode.inc';
//require_once DRUPAL_ROOT . '/includes/file.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Whenever i am trying to use filebased functions like
$wrapper = file_stream_wrapper_get_instance_by_uri('public://mypath');
$wrapper->realpath();
or
drupal_realpath('public://mypath');
the result is always
bool FALSE
Do you know what i am missing here on my custom bootstrap that file operations do not work?
Thanks
In the meantime i found the solution.
All filepaths are handled relativley internally inside Drupal.
Whenever you are using some Drupal bootstrap inside a file that is not located at the ROOT-directory, make sure to use a:
chdir(DRUPAL_ROOT);
right before your function calls.
I want to use some of the builtin symfony2 extensions(e.g:humanize,yaml_dump) for twig for a website not developed in symfony but using a twig engine.how can I do that?
The symfony/twig-bridge package provides the symfony-specific twig extensions.
These include i.e. the YamlExtension that provides the yaml_dump filter and the FormExtension that provides the humanize filter.
The extensions can be found in the Extension folder.
I strongly advise you to install the package via composer to get the package's dependencies automatically.
composer require symfony/twig-bridge:~2.3
Further composer will automatically register the classes in the autoloader (vendor/autoload.php) for you.
Now you just need to add the extensions to twig as described in the documentation.
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\YamlExtension());
// ...
A complete example, with an extension class and a quick extension (a new filter) :
<?php
require_once("vendor/autoload.php");
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
// here we add the extension class (taken from #nifr answer)
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\YamlExtension());
// here we add a new filter quickly
$filter = new Twig_SimpleFilter('paragraph', function ($argument) {
return "<p>{$argument}</p>";
}, array('pre_escape' => 'html', 'is_safe' => array('html')));
$twig->addFilter($filter);
// demo
echo $twig->render('{{ "hello" | paragraph }}');
I am trying to write a command line app using the Symfony2 Console and ClassLoader Components.
This is a screenshot of my code hierarchy and the script being called
Here is the CLI script:
#!/usr/bin/env php
<?php
require_once(dirname(__FILE__) . '/code/ClassLoader/UniversalClassLoader.php');
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use BlueHeadStudios\Command\Deployer;
$loader = new UniversalClassLoader();
$loader->register();
$input = new ArgvInput();
$debug = $input->hasParameterOption(array('--debug', ''));
$console = new Application();
$console->add(new Deployer());
$console->run();
I get this when running the script
dev#server:~/sites/pd/deployer$ php deployer.php
PHP Fatal error: Class 'Symfony\Component\Console\Input\ArgvInput' not found in /home/dev/sites/pd/deployer/deployer.php on line 13
I know it must be a simple registerNamespace call or something similar, but I've tried multiple registrations but cannot get it to work.
Any help is greatly appreciated.
I would recommend you to use composer, which will generate autoload.php for you to include at the top of the file:
#!/usr/bin/env php
<?php
require_once './vendor/autoload.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespace('BlueHeadStudios', __DIR__.'/src/');
$loader->register();
// write your code below
You should run the application with app/console check the example here
I'm trying to use MapClassLoader in autoload.php but for some reason I keep getting errors saying
Class 'Symfony\Component\ClassLoader\MapClassLoader' not found in ...\autoload.php
autoload.php:
<?php
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\ClassLoader\MapClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
//some values
));
$mapLoader = new MapClassLoader(array(
//some values
));
$mapLoader->register();
I double checked and MapClassLoader.php does exist in Symfony\Component\ClassLoader
Any idea why is it happening? :/
autoload.php is a file that configures autoloading for classes so autoloading isn't available in it and you need to include any files manually:
require_once __DIR__.'/../vendor/symfony/src/Symfony/ClassLoader/MapClassLoader.php';
Why is UniversalClassLoader available without require? Because symfony uses bootstrap file for system files to reduce file loading overhead.