Under Drupal 6 I have a custom block, which I have named and placed into a custom, right-hand sidebar region. This block's ID (which I have discovered from block.tpl.php, from the $block_id helper variable) is 7.
I am overriding the output of this block as it displays a View and I need to change the markup; I have a preprocess function in template.php called myTheme_preprocess_block() which searches for the block's unique ID thus:
myTheme_preprocess_block(&$vars) {
$this_block_id = $vars['id']; /* region-independent ID (for reliability) */
$vars['template_files'] = array();
switch ($this_block_id) {
case 7:
$vars['template_files'][] = 'block-my-override-template';
break;
default:
/* take no action */
}
}
Now, I've moved this block from the right-hand sidebar region (which is a custom region and not the default one which comes with Garland) to a footer region, which also has a custom name. And suddenly, my overriding template file, block-my-override-template.tpl.php, is no longer referenced.
I do a little digging and output the unique block ID from within block.tpl.php, and magically this block has changed its ID from 7 to 13! With a straight face, no less! Returning this block to the right-hand sidebar region also returns the block to ID 7 (and all my code starts working again).
My question is this: How can we uniquely identify a block if its "unique" ID changes when it moves from one region to another?
If you are using a View, why don't you instead override the block display of the View instead of mucking around with the actual block?
You could alternatively simply declare your custom block in a module? That should make it easier for you to manage theming aspects of the block.
Related
I've made a custom resource and I want to load it as a default in an export variable so I've tried this:
extends Node2D
export(Resource) var custom_res=CUSTOM_RESOURCE.new()
and my custom resource script as such:
extends Resource
class_name CUSTOM_RESOURCE
export var data=10
but for some reason it doesn't seem to show up within the editor:
am I doing something wrong?
shouldn't the editor display the custom resource as default whenever the scene is instanced like this?:
Running the initialization in the editor
That initialization is not running in the editor, only when the game starts.
You know the solution is tool:
tool
extends Node2D
export(Resource) var custom_res=CUSTOM_RESOURCE.new()
And all that comes with it. For people finding this form search, be aware of Engine.editor_hint.
Setting the default value
Turns out the reason we need the initializer to run is because what we are setting (CUSTOM_RESOURCE.new()) is not constant.
We can confirm that with a simple constant, for example:
export var x := 5
In that case the initializer didn't run in the editor per-se. Instead Godot set the constant was set as the default value when it parsed the script. And that is what we see in the Inspector.
So we want a constant value of our custom resource. Well, preload is a constant expression (preload is a keyword not a method). And yes, it is resolved while parsing. And yes, Godot will set it as default value, despite it not being a primitive type. Thus we can preload a resource file and use that, for example:
export var custom_res:Resource = preload("res://new_resource.tres")
Which, of course, requires that we have resource file to preload. We can create it from the context menu on the FileSystem by selecting "New Resource…", then picking our custom resource class, and a file name to save it.
Notice this differs from running the initializer in the editor in that:
It does not require tool and all that comes with that.
With tool, the default value remains (null). So you will see the arrow to reset the value in the editor, and clicking it erases the value of the property.
Your custom resource object will be the default value. So you won't see the arrow to reset the value right away. And if you modify it, resetting it the value gives you your resource object back.
Gotcha: Everywhere you preload the same resource file, you get the same resource object. Modifying it will modify it everywhere. Even across different scenes.
I need an alternate for the EditorTemplate of an Enumerator Field that's used when the Field has a particular name (PublishingMethod).
Based on the docs, I created a view with the pattern [ShapeType__FieldName] in the same folder as the original shape:
This is not working and still uses the original. I've thought of changing the Editor method in the Driver, but I think that defeats the purpose of alternates, which is that Orchard automatically detects the correct shape as I understand from the docs:
The Orchard framework automatically creates many alternates that you can use in your application. However, you can create templates for these alternate shapes.
Note: I can't use the Shape Tracing module, it never worked even with a clean Orchard install.
The editors in Orchard work different to how Display works. I guess it is so you get a MVC-style experience. Basically, the actual shape returned is of type EditorTemplate, which then binds your model and prefix then renders a partial view with the template name you gave it. What this means is alternates wont work as expected, or as the docs state. The alternates for your field name are actually added to the EditorTemplate shape. So what you can do is add a view called EditorTemplate-PublishingMethod.cshtml with contents like:
#{
var m = (Orchard.Fields.Fields.EnumerationField)Model.Model;
}
#Html.Partial("PublishingMethodEditor", m, new ViewDataDictionary {
TemplateInfo = new TemplateInfo { HtmlFieldPrefix = Model.Prefix }
})
Then add another view called PublishingMethodEditor.cshtml with the overrides you want for your editor. All these views should go in the root of your Views folder.
Another approach would be to implement the IShapeTableProvider interface and adjust the TemplateName property on a certain condition but, meh, that requires code...
Edit 1
If you have that field name on other content types that you don't want to override you can use the override EditorTemplate-ContentTypeName-PublishingMethod.cshtml
In orchard, I've added a boolean field called "IsDone" to the built in Content Menu Item content part via that Admin interface. I've then picked an item in Navigation and set the option to "yes" for the corresponding field i added.
In my custom theme, I've copied over MenuItem.cshtml.
How would I get the value of my custom "IsDone" field here?
I've tried something like
dynamic item = Model.ContentItem;
var myValue = item.MenuItem.IsDone.Value;
but I'm pretty sure my syntax is incorrect (because i get null binding errors at runtime).
thanks in advance!
First i suggest you use the shape alternate MenuItemLink-ContentMenuItem.cshtml instead of MenuItem.cshtml to target the content menu item directly.
Secondly, the field is attached to the ContentPart of the menu item. The following code retrieves the boolean field from this content part:
#using Orchard.ContentManagement;
#using System.Linq;
#{
Orchard.ContentManagement.ContentItem lContentItem = Model.Content.ContentItem;
var lBooleanField = lContentItem
.Parts
.Where(p => p.PartDefinition.Name == "ContentMenuItem") // *1
.SelectMany(p => p.Fields.Where(f => f.Name == "IsDone"))
.FirstOrDefault() as Orchard.Fields.Fields.BooleanField;
if (lBooleanField != null)
{
bool? v = lBooleanField.Value;
if (v.HasValue)
{
if (v.Value)
{
#("done")
}
else
{
#("not done")
}
}
else
{
#("not done")
}
}
}
*1
Sadly you cannot simply write lContentItem.As<Orchard.ContentManagement.ContentPart>() here as the first part in the part list is derived from this type, thus you would receive the wrong part.
While #ViRuSTriNiTy's answer is probably correct, it doesn't take advantage of the power of the dynamic objects that Orchard provides.
This is working for me but is a much shorter version:
#Model.Text
#{
bool? IsDone = Model.Content.ContentMenuItem.IsDone.Value;
var IsItDoneThough = (IsDone.HasValue ? IsDone.Value : false);
}
<p>Is it done? #IsItDoneThough</p>
You can see that in the first line I pull in the IsDone field using the dynamic nature of the Model.
For some reason (I'm sure there is a good one somewhere) the BooleanField uses a bool? as its backing value. This means that if you create the new menu item and just leave the checkbox blank it will be null when you query it. After you have saved it as checked it will be true and then if you go back and uncheck it then it will have the value false.
The second line that I've provided IsItDoneThough checks if it has a value yet. If it does then it uses that, otherwise it assumes it to be false.
Shape Alternate
#ViRuSTriNiTy's other advice, to change it to use the MenuItemLink-ContentMenuItem.cshtml instead of MenuItem.cshtml is also important.
The field doesn't exist on other menu items so it will crash if you try to access it. Just rename the .cshtml file to fix this.
Dynamic Model
Just to wrap this up with a little bit of insight as to how I got there (I'm still learning this as well) the way I figured it out is as follows:
.Content is a way of casting the current content item to dynamic, so you can use the dynamic advantages with the rest of line;
When you add the field in the admin panel it looks like it should be right there on the ContentItem, however it actually creates an invisible ContentPart to contain them and calls it whatever the ContentItem's type is.
So if you had added this field to a Page content type you would have used Model.Content.Page.IsDone.Value. If you had made a new content type called banana it would be Model.Content.Banana.IsDone.Value, etc.
Once you are inside the "invisible" part which holds the fields you can finally get at IsDone. This won't give you the actual value yet though. Each Field has its own properties which you can look up in the source code. the IsDone is actually a BooleanField and it exposes its data via the Value property.
Try doing a solution-wide search for : ContentField to see the classes for each of the fields you have available.
Hopefully this will have explained things clearly but I have actually written about using fields in a blog post and as part of my getting started with modules course over on the official docs (its way down in part 3 if you're curious).
Using built-in features instead of IsDone
This seems like a strange approach to do it this way. If you have a Content Item like a Page then you can just use the "Show on a menu" setting on the page.
Go to admin > content > open the page > down near the bottom you will find "Show on a menu":
This will automatically put it into your navigation and then you can move it around to where you want:
After it "IsDone" you can just go back and untick the "Show on a menu" option.
Setting up the alternative .cshtml
To clarify your comments about how to use the alternative, you need to
Copy the file you have at Orchard.Core/Shapes/Views/MenuItem.cshtml over to your theme's view folder so its /Views/MenuItem.cshtml
Rename the copy in your theme to MenuItem-ContentMenuItem.cshtml
Delete probably everything in it and paste in my sample at the start of this post. You don't want most of the original MenuItem.cshtml code in there as it is doing some special tricks to change itself into a different shape which isn't what you want.
Reset your original Orchard.Core/Shapes/Views/MenuItem.cshtml back to the factory default, grab it from the official Orchard repository
Understanding the view names
From your comments you asked about creating more specific views (known as alternates). You can use something call the Shape Tracer to view these. The name of them follows a certain pattern which makes them more and more specific.
You can learn about the alternates on the official docs site:
Accessing and Rendering Shapes
Alternates
To figure out what shape is being used and what alternates are available you can use the shape tracing module which is documented here:
Getting Started with Shape Tracing
How can/should I access a repeat control from outside? In my case I want to calculate some additional value(s) for the items in the repeat control and update each item with jquery.
In SSJS you always access a component with getComponent("id"). If your contents of the repeat are just a view than define the view data source at the XPage level, not the repeat and you can use the id of the datasource to get the view object, ie, view1.
From SSJS you can access the repeat control itself via getComponent(). However, if you want to access components within the repeat, it gets more challenging.
The default behaviour is to create a single "row" of components for the repeat and then, in the Render Response phase, iterate over them to generate the relevant HTML for the appropriate number of rows (rows property of the repeat control) from the relevant start point (first property of the repeat control, which may have been incremented by a pager), picking the data from the relevant data (value property of the repeat).
If the repeat has repeatControls="true" set, at page load the runtime creates a set of controls for the appropriate number of rows (rows property of the repeat control). However, accessing them may still be a challenge. This will also remove the ability to use a pager.
If you want to manipulate e.g. the appropriate view entries within the repeat, one option might be to load into viewScope the UNID / Note ID via a computed property within each row. E.g.
rendered="#{javascript:if (view.isRenderingPhase()) {
if (idex == 1) viewScope.put("myVar", new java.util.ArrayList());
viewScope.get("myVar").add(entry.getNoteID());
return true;
} else {
return true;
}"
This initialises (or re-initialises) the viewScope variable if it's the first row (please double-check first row has idex as 1, idex being the variable name you define for the indexVar property of the repeat control), and adds the NoteID to the list. So element 0 is the NoteID of the first row, element 1 the Note ID of the second row etc.
If you wish to manipulate values, you may be able to take a similar approach by using dynamic binding. There are various questions on StackOverflow about dynamic binding for repeat controls as well as blog posts, probably by either Tim Tripcony or Jesse Gallagher.
Beyond this, CSJS may be the easiest way to manipulate the contents of the repeat. Just be aware that if you do a partial refresh whose refreshId is the repeat, it may replace whatever you've done client-side.
** New EDIT **
so what I'm trying to do is this.
I want the to add new form elements generated by my module on the product view of the following url
http://magento.example.com/catalog/product/view/id/46
ultimately these elements will be determined to show up by a related table in my module
I expected that if I extended Mage_Catalog_Block_Product_View in my module as shown below I would be able to create a block in the product form that would contain such form fields, only if he are in the related table in my module
so I created a test.phtml file in
app/design/frontend/default/default/templates/<module>/test.phtml
then as you can see in my the View.php file described bellow I built the block and displayed it in the product view.
It did appear but 5 times too many. from the answers below this is normal so that answers the question as to why the it shows up five times but leaves the question what is the proper way to proceecd since this plan is not going to work
** End New Edit **
in my module I call _prepareLayout() and it does this 5 times when i pull up the page
here's my code
in
/app/code/local/Namespace/Module/Product/Veiw.php
class <Namespace>_<module>_Block_Product_View extends Mage_Catalog_Block_Product_View {
protected function _toHtml() {
return parent::_toHtml();
}
public function _prepareLayout() {
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'my_block_name_here',
array('template' => '<module>/test.phtml')
);
if ($block){
$this->getLayout()->getBlock('content')->insert($block)->toHtml();
}else{
echo "no block";
}
return parent::_prepareLayout();
}
}
NOTE:
I just noticed this also takes away the price availability qty and add to cart button. which is also a problem
EDIT
First I want to thank you all for your answers. Second i want to give you more context
the reason for choosing to do this in the module is that I don't want the block to show up on every product . What i have is a table of what I'll call custom options containing properties of the product sort of like hair color height weight etc and depending on what set of properties are attached to the product (if any) will depend on what html content will show up on the page.
so in one case it my get a drop down menu and in another case it may get an input box. the other very important piece is that this must be setup so that I can give the end result out as a module that can be installed and not worrry that it won't show up if someone upgrades there magento
that said does it still make sense to do this all in the xml file ?
It seems to me that your code is overriding a core Magento module in order to achieve what could be easily done in the layout xml configuration. I would strongly recommend the follwing:
Use the built-in configuration mechanisms (e.g. layout xml - read Alan's excellent tutorial here) instead of writing code whenever possible.
Don't override the core code
if you must change the behaviour of the core code, use an Observer rather than Rewrite/Override
if you absolutely must Override, always call parent::whatever()
For example, if you create a <module>.xml layout file in your theme (app/design/frontend/default/<theme>/layout), you could use the following code:
<catalog_product_view>
<reference name="content">
<block type="module/block" name"my_block_name_here" template="module/test.phtml"/>
</reference>
</catalog_product_view>
You would then need to use a getChildHtml('my_block_name_here'); call within your phtml to position the block.
So unless there is other functionality happening inside your _prepareLayout, there's no need to override the core, or even to override the default catalog.xml.
EDIT (small edit above)
So now in your Block (I would recommend that you call it Namespace_Module_Block_Product_Customattributes or something like that), you are not overriding the core Product_View block, but merely processing your logic for what html widgets to use to render your custom attributes. Leave the rest of the tier prices, add to cart, other generic product block code, etc to Magento to work out.
If you are worried about the upgrade path for your module's users, you should definitely NOT be overriding core code. Use the configuration approach and very selectively introduce code that "plays nice" with the system rather than try to boss it around with overrides.
I took a look at a stock Magento install of CE 1.4.1, and unmodified the _prepareLayout method is called six times when loading the URL
http://magento.example.com/catalog/product/view/id/46
That's because the class is instantiated six times. So that's the correct behavior.
As for the vanishing element, I can'y say for sure, but your override to _prepareLayout doesn't appear to either
Do the same things as Mage_Catalog_Block_Product_View::_prepareLayout
Call parent::_prepareLayout();
When you override a class in a Magento you're replacing an existing class with your own. If you change a method, you're responsible for that old code being run.
It's not clear what you're trying to accomplish here. You should consider breaking your problem down into smaller problems, and then posting one (or more) "I tried X, expected Y, and got Z" type questions. As written no one's going to be able to answer your question.