Вопрос

I'm attempting to grab a node from a node ID in an external php script. With Drupal 7 I used the bootstrap.inc but with Drupal 8 this are a bit different.

I've called Drupal with:

define('DRUPAL_DIR', $webroot);

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Drupal\file\Plugin\Field\FieldType;

$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();

require_once DRUPAL_DIR . '/core/includes/database.inc';
require_once DRUPAL_DIR . '/core/includes/schema.inc';

I'm then successfully loading the node with:

$node = \Drupal\node\Entity\Node::load($nid);

I'm able to grab values with:

$node->get('field_test_field')->value

but I'm unable to get a uri for images. If I try and get the target_id with:

$node->get('field_image_field')->value

I get an error and the following in the error log:

Error: Call to undefined function Drupal\\file\\Plugin\\Field\\FieldType\\file_default_scheme() in /var/www/vhosts/rootpath/webroot/core/modules/file/src/Plugin/Field/FieldType/FileItem.php on line 39

Any idea how I get get the image path? The returned node object includes:

[field_image_field] => Array
            (
                [x-default] => Array
                    (
                        [0] => Array
                            (
                                [target_id] => 44595
                                [alt] => 
                                [title] => 
                                [width] => 250
                                [height] => 89
                            )

                    )

            )

Not sure what I missing, perhaps I need to include a namespace or something? Thanks in advance!

Это было полезно?

Решение

I had the exact same problem with an external script, but with the field_files field.

I can see it in when doing a print_r on the node object:

        [field_files] => Array
            (
                [x-default] => Array
                    (
                        [0] => Array
                            (
                                [target_id] => 2
                                [display] => 1
                                [description] => 
                            )

                    )

            )

But trying to access it via $node->field_files->value gives:

Fatal error: Call to undefined function Drupal\file\Plugin\Field\FieldType\file_default_scheme() in /[redacted]/core/modules/file/src/Plugin/Field/FieldType/FileItem.php on line 39

I can access other fields in this manner just fine. Also, doing a $node->getFields() should return an array of all the fields, but this throws the same error.

I solved the problem by including a file:

require_once DRUPAL_DIR . '/core/includes/file.inc';

And then I can access the file id with:

$node->field_files->target_id;

So I am able to get the uri of file attached to the node with:

$fid = $node->field_files->target_id;
$file = \Drupal::entityManager()->getStorage('file')->load($fid);
$uri = $file->uri->value;

It might be a little different for your image field, but including that file should stop that error.

Другие советы

you can try:

$image_uri  = $node->field_image_field->entity->getFileUri();

echo $image_uri;

$image_url= file_create_url($image_uri);

echo $image_url;

in the variable "$ image_uri" contains the uri of the image.

in the variable "$ image_url" contains the complete url of the image.

Note: please, change field_image_field for your machine name field

Лицензировано под: CC-BY-SA с атрибуция
Не связан с drupal.stackexchange
scroll top