When using CSSPackageResource.getHeaderContribution(ResourceReference) or JavascriptPackageResource.getHeaderContribution(ResourceReference) is there a way to see if the css or JavaScript file actually exist before calling add?
On PackageResource, the parent of CSSPackageResource and JavascriptPackageResource there is method exists() which is defined:
exists
public static boolean exists(Class scope,
String path,
Locale locale,
String style)
Gets whether a resource for a given set of criteria exists.
Parameters:
scope - This argument will be used to get the class loader for loading the package resource, and to determine what package it is in. Typically this is the class in which you call this method
path - The path to the resource
locale - The locale of the resource
style - The style of the resource
Returns: true if a resource could be loaded, false otherwise
Just check out the documentation
Related
My project requires me to know what file extension was used while calling a route.
For example,
If the route was 127.0.0.1/controller/action/filea.json
Then then I would need to have a function that returns ".json" when called from inside action function "get_action".
If the route was 127.0.0.1/controller/action2/fileb.xml
Then then the function should return ".xml" when called from inside action function "get_action2".
Right now, I tried using \URI::current(), but that only gets me "127.0.0.1/controller/action/filea" or "127.0.0.1/controller/action2/fileb"
Whether or not the extension is used is controlled by the config key routing.strip_extension, which is true by default.
The current extension can be retrieved using \Input::extension().
We have some sites such as:
/apps/site1
|----i18n
|---data
|---keys
|---languages
|---translator
/apps/site2
|----i18n
|---data
|---keys
|---languages
|---translator
Now when we hit the site, we can see the call libs/cq/i18n/dict.xx-xx.json. Ideally that call should be either from /apps or /etc. How can I achieve this?
OOTB servlet :
#Component
#Service({Servlet.class})
#Properties({#Property(
name = "service.description",
value = {"ResourceBundle Renderer Servlet"}
), #Property(
name = "sling.servlet.paths",
value = {"/libs/cq/i18n/dict"}
)})
public class ResourceBundleExportServlet extends SlingSafeMethodsServlet {
This is not possible, as of aem 6.3 (and earlier versions) that path was hard-coded pretty much everywhere it was used. to use a custom path you will have to-
Register custom servlet at /apps/cq/i18n/dict.xx-xx.json (or path of your choice) to return i18n values.
Change AEM ootb code that uses the hardcoded path to use your custom path.
I want to get Some Property from Source Node Using getProperties() and setProperty in another Destination Node. How can i check that property is protected or not.As if i copy all the property in destination it gives me ConstraintViolationException
You'd need to get the definition of the property:
PropertyDefinition propDefinition = node.getProperty("/yourprop").getDefinition();
on the definition you can call isProtected():
Boolean isPropertyProtected = propDefinition.isProtected();
or just inline it:
node.getProperty("/yourprop").getDefinition().isProtected();
for further reading I suggest:
http://www.day.com/specs/jcr/2.0/16_Access_Control_Management.html;
Chapter 16.3.12 Interaction with Protected Properties
And the JCR documentation on node types:
http://jackrabbit.apache.org/jcr/node-types.html
This is probably because you are trying to copy all properties which includes cq:primaryType as well. If you see in crx, these basic properties are not editable.
For copy you can take a specific property and set a specific property, instead of copying and pasting all the properties.
i wrote own decorator in the app path like "library/myLib/Form/Decorator/Lalala.php"
now suppose to use it like $element->addDecorator('Lalala');
but get error:
Plugin by name 'Filechoose' was not found in the registry; used paths:
Zend_Form_Decorator_: Zend/Form/Decorator/
how to set up default element decorator paths.
Assuming that your decorator class myLib_Form_Decorator_Lalala is stored in file library/myLib/Form/Decorator/Lalala.php, then we can do it as follows:
At the form level:
$form->addElementPrefixPath(APPLICATION_PATH . '/../library/myLib/Form/Decorator', 'myLib_Form_Decorator_', Zend_Form::DECORATOR);
At the element level:
$elt->addPrefixPath(APPLICATION_PATH . '/../library/myLib/Form/Decorator', 'myLib_Form_Decorator_', Zend_Form::DECORATOR);
I have a view helper being called in my layout that works fine in the default module, but I get an exception when I am in another module.
I have already changed my app.ini to use the default layout across all modules by setting:
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
And searching here and google provided me with another app.ini setting to add the view helper path for all modules:
resources.view.helperPath.Zend_View_Helper = APPLICATION_PATH "/views/helpers"
However instead of fixing the problem, that additional setting causes the Zend Exception to become a WSOD.
Without that second app.ini setting, I see the layout and get this exception:
Plugin by name 'AutoScript' was not found in the registry; used paths: Admin_View_Helper_: /Applications/XAMPP/xamppfiles/htdocs/dad/application/modules/admin/views/helpers/ Zend_View_Helper_: Zend/View/Helper/:./views/helpers/
With the helperPath.Zend_View_Helper ini setting I get a WSOD with the following:
Fatal error: Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'AutoScript' was not found in the registry; used paths: Zend_View_Helper_: Zend/View/Helper/:./views/helpers/'
It appears the plugin loader is looking in public/views/helpers/ for the AutoScript.php file even though it should be using the APPLICATION_PATH value as a prefix.
My layout invocation looks like this
<?php $this->AutoScript(); ?>
My AutoScript.php file's class is defined in application/views/helpers/
class Zend_View_Helper_AutoScript extends Zend_View_Helper_Abstract {
public function AutoScript() {...}
}
My current fix is to copy the AutoScript.php file from application/views/helpers into modules/admin/views/helpers which fixes the issue, but duplicates a file. What am I missing? Do I have to add this view helper path programmatically by creating an _initView function in my bootstrap?
Typically, you would name your custom view helper with your own prefix, not the Zend_ prefix. Beyond that, there are several choices for where to put and name your view helpers.
If this view-helper is really a single-application helper, then I find it natural for it to reside somewhere in the application folder. Within that possibility space, I would ask if the view-helper will be used in a single module or across multiple modules.
If the view-helper is intended for use in a single module, then I depend upon the build-in resource-autoloader mappings and place my view-helper class Mymodule_View_Helper_Myhelper in the file application/modules/mymodule/views/helpers/Myhelper.php.
If the view-helper is intended for use across multiple modules , I might pull it up a little higher than the modules folder, say Application_View_Helper_Myhelper (assuming an appnamespace of Application) stored in
application/views/helpers/Myhelper.php. In this case, we need to tell the view that there are helpers with prefix Application_View_Helper_ in that directory. This can be done at bootstrap:
protected function _initViewHelperPaths()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Application_View_Helper_');
}
Sometimes, you need a view-helper in one module that exists in another module and you cannot - in practical terms - move the original one around. In that case, one workaround is to define an empty shell of a view-helper in your consuming module extending your unmovable view-helper. In file application/mymodule/views/helpers/MyHelper.php:
class Mymodule_View_Helper_Myhelper extends Othermodule_View_Helper_Myhelper
{
}
This way, the code for the helper implementation is not duplicated. The module-specific resource-autoloader mappings will allow all those classes to be found when they invoked as view-helpers from a view-script.
Finally, if this view helper is to be used in multiple projects, then there is an argument for placing it outside of application scope, out in in the library folder. So perhaps a class MyLibrary_View_Helper_Myhelper stored in the file library/MyLibrary/View/Helper/Myhelper.php. As before, you would need to inform the view - probably at bootstrap or in a front-controller plugin - of the prefix/path mapping:
$view->addHelperPath(APPLICATON_PATH . '/../library/MyLibrary/View/Helper', 'MyLibrary_View_Helper_');
Note that in all cases above, the invocation of the view-helper functionality itself - say, inside a view-script - is:
<?php echo $this->myhelper($params) ?>
Note, in particular, the casing difference between the classname and the invocation.