0

I have a php file in my Drupal 8 module.The name of my custom module is fb. Within the fb folder I have a php file fb.php .The content of that file is loaded as an iframe using the follwing code

 public function buildForm(array $form, FormStateInterface $form_state) {
  $extpath = take_control_fb_ext_client_path();
  $fbpath = take_control_client_path(drupal_get_path('module', 'fb') . '/fb.php?extpath=' . $extpath);
  global $base_url;
  $form['#attached']['take_Control_fb']['library'][] = 'fb/fb';
      $form = array(
      'take_control_fb_container' => [
    '#type' => 'inline_template',
    '#template' => '',
    '#context' => [
      'url' => '/modules/fb/fb.php?extpath=/libraries/extjs/ext-3.4.1'
  ],
],
); return $form; }

.I need to check drupal login is set or not in the fb.php file. I have used the following code to check that

$logged_in = \Drupal::currentUser()->isAuthenticated();

Ref link : https://docs.acquia.com/articles/there-drupal-function-check-if-user-logged

But it resulted the error 'PHP Fatal error: Class 'Drupal' not found'.

Please help me to figure out some solution to fix this.

2
  • 1
    The code is OK to check if the user is logged in. But the external php file is not part of drupal and this does not change by moving it in the drupal file system.
    – 4uk4
    Oct 6, 2016 at 9:58
  • 4k4 is right. If you set this php-File as src for your iframe it won't be part of your module. That's why Drupal isn't bootstraped and the class Drupal unknown. Oct 6, 2016 at 10:03

1 Answer 1

0

The correct approach for this would be to implement a service, in which you put the contents of the fb.php and there all the Drupal functions will be recognized. Here is an example of a service implementation: https://docs.acquia.com/articles/drupal-8-services-dependency-injection-and-service-containers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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