I have extended CMS settings to upload two CSS files. Part of the SiteConfig extension code -
<?php
//OTHER CODES HERE
$fields->addFieldToTab('Root.Main', $cssfile = UploadField::create(
'StyleSheet',
'Style Sheet'
));
$fields->addFieldToTab('Root.Main', $clientcssfile = UploadField::create(
'ClientStyleSheet',
'Client Style Sheet'
));
//OTHER CODES HERE
?>
And i have extended the File class
class FileRenameExtension extends DataExtension {
function onAfterUpload() {
$file = $this->owner;
$file->Name = 'CustomStyle.'.$file->getExtension();
$file->write();
}
}
PROBLEM
The code renames the file uploaded from all the fields, but I want to rename the files uploaded from ClientStyleSheet only.
Before it renames the file, if user uploads file with name that already exists, it warns, and if I click Overwrite, it overwrites the files and renames the file. I just want no matter what is the file name, it just saves the file with name that is specified in the File Extension Class
What is the proper way to rename file before its get uploaded?
You better use FileField directly in your case with custom upload() action handler.
With the DataExtension, you modify all instances of File, wherever they are uploaded.
One possible solution: You subclass File and hook into onAfterUpload:
class CSSFile extends File {
function onAfterUpload() {
$file = $this;
$file->Name = 'CustomStyle.'.$file->getExtension();
$file->write();
parent::onAfterUpload();
}
}
Now you use this in your SiteConfig, it should work with UploadField just like a standard File.
If I understand it correctly, you want to enable the user to add a stylesheet to the site. A different solution would then be to allow any filenames and to use the original filename in the template:
<link rel="stylesheet" href="$SiteConfig.StyleSheet.URL">
I would prefer to use this second solution.
Related
I am using the Profile Builder Pro Plugin to let users upload files to Wordpress. So the files get uploaded to the standard Wordpress upload folder.
However I need to change the upload directory to a subfolder of the uploads folder, but only if the files are uploaded from a certain front end page.
Is there a way I can do this?
Here is the example I use when uploading my custom avatars. Basically you need to add a filter when wp_handle_upload is executed and remove it afterwards, you will also need to add your conditions and generally complete the code to handle posted files, attach them to user or post etc.
//UPLOAD FOLDER FILTER FOR AVATAR
function avatar_upload_dir( $dirs ) {
$dirs['subdir'] = '/avatars';
$dirs['path'] = $dirs['basedir'] . '/avatars';
$dirs['url'] = $dirs['baseurl'] . '/avatars';
return $dirs;
}
//SOME CUSTOM UPLOAD FUNCTION
function somefunction() {
//some code here
add_filter( 'upload_dir', 'avatar_upload_dir' );
$uploaded_file = wp_handle_upload( $data['file'], array( 'test_form' => false ) );
remove_filter( 'upload_dir', 'avatar_upload_dir' );
//some code here
}
You can create new folder media in wordpress directory, then edit the wp-config.php file and this line before
define('UPLOADS', 'media');
/*Thats all, stop editing!, Happy blogging*/
For more detail check this link
I would like my custom Drupal 8 module to force a different template for pages that match a certain URL. Here is my module structure:
/sass_edit
/css
/js
/template
builder_template.html.twig // this is the template file
sass_edit.module
...
sass_edit.*.yml // various yml files
These are the contents of builder_template.html.twig:
<h1>test</h1>
Here are the relevant lines of code in my .module file:
function sass_edit_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
$current_path = \Drupal::service('path.current')->getPath();
if(\Drupal::currentUser()->isAuthenticated() && strpos($current_path, "/builder/") >= 0) {
$suggestions[] = 'builder_template';
}
// kint($suggestions);
// die();
}
When I visit a page whose URL contains /builder/, the code runs and adds the string builder_template to the $suggestions array. However, when the page is rendered, the new template is ignored. I have tried flushing caches, with no results.
I'm not sure how to proceed. All the documentation I've found refers to adding theme suggestions from a custom theme, not a custom module.
Any idea what I'm doing wrong? Thank you so much for any help!
Maybe you have a typo in the code you pasted but the folder where you have your templates overrides should be templates and not template as you wrote. Don't forget to flush caches after changing the name of the directory ;)
I have removed the base_tag from my template to allow for the use of SVG icon sprites. A side effect of this is that images on pages other than the home page break.
The HTMLEditorField::saveInto() method is forcing a relative url on the image and they end up pointing to /some-page-other-than-home/assets/image.jpg which is wrong.
How do I get images to resolve to the root /assets/ directory rather than pointing to /some-page-other-than-home/assets/.
SilverStripe provides extension hooks on some methods. The HTMLEditorField has a 'processImage' hook that you can tap into.
You can hook into this method with the following:
1. Create your extension configuration.
/mysite/_config/Config.yml
HtmlEditorField:
extensions:
- HTMLEditorFieldExtension
2. Create a the HTMLEditorFieldExtension class.
/mysite/code/HTMLEditorFieldExtension.php
<?php
class HTMLEditorFieldExtension extends DataExtension
{
// This method name must be the same as the extension hook
public function processImage($image, $img) {
// Get the image src attribute
$src = $img->getAttribute('src');
// Concatenate a leading slash
$img->setAttribute('src', '/' . $src);
}
}
3. Run a dev/build.
This needs to be done so SilverStripe can find the new extension. After this your images should now have the missing leading slash.
I want to remove the Login or register to post comments text from the page where I created a webform; is there any suggestion as to how can i use hook_link_alter() with this?
This code resides in comment.module file under the theme_comment_post_forbidden() function.
If you are using Drupal 7, you may use hook_node_view_alter or hook_entity_view_alter to modify the displayed content.
function foo_node_view_alter (&$build) {
if ($build['#node']->type == 'webform') {
// remove login or register to post comments
unset($build['links']['comment']['#links']['comment_forbidden']);
// remove add comments
unset($build['links']['comment']['#links']['comment_add']);
}
}
In case you want to use hook_link_alter in Drupal 6, use this code in your custom module
function comment_link_alter (&$links, $node) {
if ($node->type == 'webform') {
// remove register or login to post comments
unset($links['comment_forbidden']);
// remove add a comment
unset($links['comment_add']);
}
}
If you are working with a content type, you could over-ride the theme.
copy the template file '/modules/node/node.tpl.php' into your theme's 'templates' directory
rename the file, calling it 'node--NODETYPE-tpl.php' (that's two hyphens after 'node'). For example, 'node--book-tpl.php' for a 'book' content type.
comment out the final two lines (or delete them):
// print render($content['links']);
// print render($content['comments']);
I'm working on a new Moodle Assignment plugin.
How can I include a custom CSS to my plugin?
I'm using Moodle 1.9.7.
Thanks in advance.
just add a file called styles.php into the Module's folder.
the file should output CSS code when it is parsed.
Moodle goes into each Module's folder and looks for that file, when the page's CSS layout is constructed.
Some themes can ignore these files using a special setting in the theme's config.php file
$THEME->modsheets = true;
/// When this is enabled, then this theme will search for
/// files named "styles.php" inside all Activity modules and
/// include them. This allows modules to provide some basic
/// layouts so they work out of the box.
/// It is HIGHLY recommended to leave this enabled.
here is a short sample of what could be the content of this styles.php file:
/* simple CSS code */
.left { float:left; }
/* php that generate CSS code */
< ?php if ( right_to_left() ) {echo ".right:text-align:left;"}
else {echo ".right:text-align:right;"} ?>
Perhaps could take a look at the function print_header_simple() defined in lib/weblib.php.
If you are writing a module which generates module specific pages, and they use the normal moodle libraries then stick something in $meta to be added to the <head> section while the page is being generated.
in lib/weblib.php:
function print_header_simple($title='', $heading='', $navigation='', $focus='', $meta='', $cache=true, $button=' ', $menu='', $usexml=false, $bodytags='', $return=false) {
This means that you probably want something like the following in your module:
$extra_css = $CFG->wwwroot.'/theme/SUPA4/supa_report.css';
$extra_css_meta = '<link rel="stylesheet" type="text/css" href="'.$extra_css.'" />';
print_header_simple($mytitle, "", $navigation, "", "", true, "", navmenu($course), '', $extra_css_meta);