-1

I've got a form with a #markup element and I'm trying to use a php file as the contents of the markup. The following is printing a 1 where the markup should goes, and printing the contents of the file as the first elements in my page's body tag.

$path = drupal_get_path('theme', 'my_subtheme');
$template = "$path/templates/myfile.php";
$form['membership_info'] = array(
  '#markup' => include $template,
  '#weight' => 0,
);

I understand that the include line is probably including the file at the top of the page, and the markup is rendering as 1 because include $template is returning true, I'm just not sure how to get my desired outcome. Any ideas as to how I can get it to render how I'm expecting it to?

I was using a custom template for the entire form but that was stopping it from submitting. As such I'm instead trying to work my way around it by using this markup element to contain all of the non-form-field logic at the top of my form.

1 Answer 1

0

Considering myfile.php contains html :

$template = file_get_contents("$path/templates/myfile.php");
$form['membership_info'] = array(
  '#markup' => $template,
  '#weight' => 0,
);

If you are using variables in myfiles.php, use ob_start() & ob_end_flush() to get its content.

Read this, what you are trying to do is not really in agrement with drupal philosophy (probably reason of someone's downvote). It looks quiet strange to do includes while drupal 7 offers many ways to do this differently.

1
  • I ended up abandoing this strategy and setting up a custom block/template intstead. Coming across includes in site audits always frustrates me so I figured it best to avoid it in the end. Thanks for the suggestion though.
    – Mrweiner
    Commented Nov 23, 2017 at 10:50

Your Answer

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

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