browse the blogs

[ 23 Jan 2016 ]

I’m seeing the following error a lot when running bin/magento setup:upgrade

Missing write permissions to the following directories: ’/Users/alanstorm/Sites/magento-2-with-keys/magento2/pu

Extra frustratingly – if I look at every file and directory in pub/static, they all have fully world readable, writable, and executable permissions. (this isn’t my favorite way to run mod_php, but Magento 2 seems to have left us non-FastCGI dinosaurs on our own)

The only way I’ve found to fix this is to delete my entire pub/static directory and let Magento regenerate it.

I finally got to the bottom of this today. It looks like Magento’s permissions checking code will resolve symlinks to their final folder when checking permissions.

If you’re working your way through my

[ 21 Jan 2016 ]

These methods aren’t, as of 2.01, marked as @api safe so they may go away. That said, they’re pretty integral to how the layout works so I feel like they have a good shot of sticking around.

If you want to add a frontend asset to your page layout programmatically and not render the <link/>/<script/> tags yourself, you’ll want to use the asset repository and the asset grouped collection

\Magento\Framework\View\Asset\Repository $assetRepository
\Magento\Framework\View\Asset\GroupedCollection $assetCollection

Specifically, use the asset repository to create an asset

$identifier = 'css/somefile.css';
$asset      = $assetRepository->createAsset($identifier)    

and then add the asset to the grouped collection


Unfortunately, you can’t use this technique from a block’s _prepareLayout statement. Magento 2 renders a page in the following method

#File: ...

[ 16 Jan 2016 ]

Not strictly a Magento 2 thing, but if you have code that looks like this in your extension

namespace Foo\Baz\Bar;
if( $node instanceof SimpleXMLElement) {}

You’ll need to make sure that SimpleXMLElement is actually used in your current namespace.

namespace Foo\Baz\Bar;
use SimpleXMLElement
if( $node instanceof SimpleXMLElement) {}

Otherwise the instanceof operator will be looking for an instance of Foo\Baz\Bar\SimpleXMLElement, and not SimpleXMLElement.

[ 12 Jan 2016 ]

Quick heads up if you’re using Magento 2’s __ function to translate strings in your extensions. Magento 1’s translation functions allowed you to use sprintf style replacement tokens.

Mage::helper('module')->__('Hello %s', $string);
$this->__('Hello %s', $string);    

Magento two opts to use a globally accesible __ function.

$translate_string = __('Hello World');    

This new function is not sprintf compatible. It does support replacement tokens, but those tokens are numerical based instead of type based.

$translate_string = __('Hello %1, how are %2', $string, $foo);    

Substitute your own treaties on the appropriateness of sprintf existing in a language that allows you to concatenate any two things together and get a string out the other side automatically.

[ 12 Jan 2016 ]

Using PHP Code to Generate Asset URLs in Magento 2:

Question and answer from yours truly on using the asset repository to get the full URL to Magento 2 frontend asset files.

The short version – inject a Magento\Framework\View\Asset\Repository, use $repository->createAsset('Vendor_Module::path/to/file.js') to create an asset object, use the getUrl method on that created asset object.

[ 8 Jan 2016 ]

Based on some early theme conversion work in Magento 2 – it looks like the view.xml theme fallback is an all or other thing. If your new theme’s view.xml file is missing an image that’s defined in the parent theme file, Magento explodes when trying to find the parent image file.

[ 7 Jan 2016 ]

Can’t Create URLs Shorter than Three Characters · Issue #2910 · magento/magento2:

Reminder: In Magento 2 (as in Magento 1) a route’s name is how Magento internally identifies the route, and uses it for creating things like layout handles. The front name is what shows up in the URL bar. These are very often the same, but don’t need to be the same.

[ 7 Jan 2016 ]


Nice sample extension with a lot of Magento 2 features.

[ 6 Jan 2016 ]

Can’t Create URLs with Single Dashes · Issue #2910 · magento/magento2:

One of the downsides of creating a new framework is you end up creating the same problems that have already been solved in other frameworks. For some reason Magento 2 wants all URL “front names” (the first URL segment) to be three characters long, and not contain any dashes.

[ 6 Jan 2016 ]

Magento 2: Different static-asset Files Per Locale?:

To save you a click