ブロックのユニークな識別子を取得する

ブロックで次のコードを実行すると

$this->getIdentifier();

オリジナルブロックとクリップボード中のブロックで同じIDが生成される。

代わりに次のように書く

$this->set(
  'blockIdentifier',
  $this->getBlockObject()->getProxyBlock()
    ? $this->getBlockObject()->getProxyBlock()->getInstance()->getIdentifier()
    : $this->getIdentifier()
);

そしてview.phpで次のように書ける

<div id="<?php echo $blockIdentifier ?>"></div>

この方法だと、クリップボードからペーストしたブロックでもユニークなIDが生成される。この方法はバージョン5.6系で使用可能。

原文:Obtain a Unique Identifier for a Block