0

Please could someone advise what is wrong with the following? When the page loads it downloads an unknown file type. The form is displayed but not with the template/theming.

The form...

function test_form($form, &$form_submit) {
    $form['message'] = array(
            '#type' => 'textarea',
            '#title' => t('Message'),
            '#required' => TRUE,
    );
    $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('Send')
    );

    // Get the path to the module
    $path = drupal_get_path('module', 'test');

    // Attach the CSS and JS to the form
    $form['#attached'] = array
    (
            'css' => array
            (
                    'type' => 'file',
                    'data' => $path . '/css/test.css',
            ),
            'js' => array
            (
                    'type' => 'file',
                    'data' => $path . '/js/test.js',
            ),
    );

    $form['#theme'] = 'test_form';
    return $form;
}

The theme...

function test_theme($existing, $type, $theme, $path) {
    return array(
            'test_form' => array(
        //'arguments' => array('form' => null),
        'path' => drupal_get_path('module', 'test') . '/templates',
        'template' => 'test',
        'render element' => 'form',
            ),
    );
}

The directory...

  • test
  • test/css/test.css
  • test/js/test.js
  • test/templates/test.tpl.php
  • test/test.info test/test.module
1
  • If possible try to read out the recent reports of your site in /admin/reports/dblog. It may open up the exact issue. I faced the same issue, I checked the recent reports, found out it occurred due to invalid path reference of the tpl file Oct 31, 2013 at 7:46

1 Answer 1

0

It sounds like there is just a problem with the loading of the attached files in your $form['#attached'] setting. Are you getting 404 errors from your debugger for the js and css files or is it not trying to load them?

You should try rewriting your form settings with the type as a settings array (if you need it) according to the API documentation

$form['#attached'] = array
(
        'css' => array
        (
                $path .'css/test.css' => array('type' => 'file',),
        ),
        'js' => array
        (
                $path .'js/test.js' => array('type' => 'file',),
        ),
);

or maybe more simply as:

$form['#attached']['css'] = array(
  drupal_get_path('module', 'test') . 'css/test.css',
);

$form['#attached']['js'] = array(
  drupal_get_path('module', 'test') . 'js/test.js',
);

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.