1

In my field.tpl.php, I have used PHP to write a string of CSS. The content of the string varies according to the field options.

I then add this string of css to my theme using drupal_add_css(). The style is then added to my theme, but it is added inline.

Is there a way to make the string of CSS to be written to an external stylesheet instead?

  • Out of curiosity, why do you need this in an external sheet? – mpdonadio Jan 5 '13 at 14:26
1

If you change the content of the CSS file every time the template file is invoked, then you could use code similar to the following one. The code is longer than it should be because I am using a custom function whenever the equivalent Drupal function would output a message error.

function mytheme_data_save($data, $destination, $replace = FILE_EXISTS_RENAME) {
  if (!file_valid_uri($destination)) {
    watchdog('file', 'The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', array('%destination' => $destination));
    return FALSE;
  }

  if ($uri = mytheme_unmanaged_data_save($data, $destination, $replace)) {
    // Create a file object.
    $file = new stdClass();
    $file->fid = NULL;
    $file->uri = $uri;
    $file->filename = drupal_basename($uri);
    $file->filemime = file_get_mimetype($file->uri);
    $file->uid      = $user->uid;
    $file->status   = FILE_STATUS_PERMANENT;
    // If we are replacing an existing file re-use its database record.
    if ($replace == FILE_EXISTS_REPLACE) {
      $existing_files = file_load_multiple(array(), array('uri' => $uri));
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $file->fid = $existing->fid;
        $file->filename = $existing->filename;
      }
    }
    // If we are renaming around an existing file (rather than a directory),
    // use its basename for the filename.
    elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
      $file->filename = drupal_basename($destination);
    }

    return file_save($file);
  }

  return FALSE;
}

function mytheme_unmanaged_data_save($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  // Write the data to a temporary file.
  $temp_name = drupal_tempnam('temporary://', 'file');
  if (file_put_contents($temp_name, $data) === FALSE) {
    return FALSE;
  }

  // Move the file to its final destination.
  return mytheme_unmanaged_file_move($temp_name, $destination, $replace);
}

function mytheme_unmanaged_file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  $filepath = mytheme_unmanaged_file_copy($source, $destination, $replace);
  if ($filepath == FALSE || file_unmanaged_delete($source) == FALSE) {
    return FALSE;
  }
  return $filepath;
}

function mytheme_unmanaged_file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  $original_source = $source;
  $original_destination = $destination;

  // Assert that the source file actually exists.
  if (!file_exists($source)) {
    if (($realpath = drupal_realpath($original_source)) !== FALSE) {
      watchdog('file', 'File %file (%realpath) could not be copied because it does not exist.', array('%file' => $original_source, '%realpath' => $realpath));
    }
    else {
      watchdog('file', 'File %file could not be copied because it does not exist.', array('%file' => $original_source));
    }
    return FALSE;
  }

  // Build a destination URI if necessary.
  if (!isset($destination)) {
    $destination = file_build_uri(drupal_basename($source));
  }

  // Prepare the destination directory.
  if (file_prepare_directory($destination)) {
    // The destination is already a directory, so append the source basename.
    $destination = file_stream_wrapper_uri_normalize($destination . '/' . drupal_basename($source));
  }
  else {
    // Perhaps $destination is a dir/file?
    $dirname = drupal_dirname($destination);
    if (!file_prepare_directory($dirname)) {
      // The destination is not valid.
      watchdog('file', 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $original_source, '%destination' => $dirname));
      return FALSE;
    }
  }

  // Determine whether we can perform this operation based on overwrite rules.
  $destination = file_destination($destination, $replace);
  if ($destination === FALSE) {
    watchdog('file', 'File %file could not be copied because a file by that name already exists in the destination directory (%directory)', array('%file' => $original_source, '%destination' => $destination));
    return FALSE;
  }

  // Assert that the source and destination filenames are not the same.
  $real_source = drupal_realpath($source);
  $real_destination = drupal_realpath($destination);
  if ($source == $destination || ($real_source !== FALSE) && ($real_source == $real_destination)) {
    watchdog('file', 'File %file could not be copied because it would overwrite itself.', array('%file' => $source));
    return FALSE;
  }
  // Make sure the .htaccess files are present.
  file_ensure_htaccess();
  // Perform the copy operation.
  if (!@copy($source, $destination)) {
    // If the copy failed and realpaths exist, retry the operation using them
    // instead.
    if ($real_source === FALSE || $real_destination === FALSE || !@copy($real_source, $real_destination)) {
      watchdog('file', 'The specified file %file could not be copied to %destination.', array('%file' => $source, '%destination' => $destination), WATCHDOG_ERROR);
      return FALSE;
    }
  }

  // Set the permissions on the new file.
  drupal_chmod($destination);

  return $destination;
}

This is how you should use the code.

if (($file = mytheme_data_save($data, 'public://mytheme_css') !== FALSE) {
  if ($instance = file_stream_wrapper_get_instance_by_uri($file->uri)) {
    $url = $instance->getExternalUrl();
    drupal_add_css($url, array('type' => 'external'));
  }
}

With the code I am using, the file used to contain the CSS styles is deleted the first time the cron tasks are executed, if the file has been created 6 hours earlier.

|improve this answer|||||
0

To add external stylesheet - you need to set type - externallike this

drupal_add_css('http://fonts.googleapis.com/css?family=News+Cycle', array('type' => 'external'));

More info about - here

|improve this answer|||||
  • 1
    How can this be used to get the value of a node's field into an external sheet? – mpdonadio Jan 5 '13 at 14:26
  • The OP is dynamically creating CSS styles, and he wants to put them in a file, rather than adding inline to the HTML output. – kiamlaluno Jan 5 '13 at 16:29
0

Seems to me like you could use something like file_put_contents (see http://php.net/manual/en/function.file-put-contents.php) to write an external CSS file and then use drupal_add_css to add the external stylesheet that you just created with PHP.

I hate inline styling too.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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