title: Magento 2 get order in custom block
body: <p>I have a custom module which sends email to custom email address after successful purchase. It works well but now I need to get gift message from order.</p>
  
  <p>Here is my Block code</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace Test\Test2\Block\Vendor\Shipment;
  
  class Items extends \Magento\Framework\View\Element\Template
  {
      const DEFAULT_TYPE = 'default';
  
      public function getItemRenderer($type)
      {
          /** @var \Magento\Framework\View\Element\RendererList $rendererList */
          $rendererList = $this-&gt;getRendererListName() ? $this-&gt;getLayout()-&gt;getBlock($this-&gt;getRendererListName()) : $this-&gt;getChildBlock('renderer.list');
          if (!$rendererList) {
              throw new \RuntimeException('Renderer list for block "' . $this-&gt;getNameInLayout() . '" is not defined');
          }
          $overriddenTemplates = $this-&gt;getOverriddenTemplates() ?: [];
          $template = isset($overriddenTemplates[$type]) ? $overriddenTemplates[$type] : $this-&gt;getRendererTemplate();
          $renderer = $rendererList-&gt;getRenderer($type, self::DEFAULT_TYPE, $template);
          $renderer-&gt;setRenderedBlock($this);
          return $renderer;
      }
  
      public function getItemHtml(\Magento\Framework\DataObject $item)
      {
          $block = $this-&gt;getItemRenderer($item-&gt;getProductType())-&gt;setItem($item);
          return $block-&gt;toHtml();
      }
  }
  </code></pre>
  
  <p>and email template .phtml</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php $_items = $block-&gt;getItems() ?&gt;
  &lt;?php $_order = $block-&gt;getOrder() ?&gt;
  &lt;?php if ($_items): ?&gt;
      &lt;table class="items email-items shipment-items order-items"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th class="item-info"&gt;
                      &lt;?= /* @escapeNotVerified */  __('Items'); ?&gt;
                  &lt;/th&gt;
                  &lt;th class="item-price"&gt;
                      &lt;?= /* @escapeNotVerified */  __('Price'); ?&gt;
                  &lt;/th&gt;
                  &lt;th class="item-qty"&gt;
                      &lt;?= /* @escapeNotVerified */  __('Qty'); ?&gt;
                  &lt;/th&gt;
                  &lt;th class="row-total"&gt;
                      &lt;?= /* @escapeNotVerified */  __('Total'); ?&gt;
                  &lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;?php foreach ($_items as $_item): ?&gt;
              &lt;?php
                  if ($_item-&gt;getParentItem()) {
                      continue;
                  }
              ?&gt;
              &lt;tbody&gt;
                  &lt;?= $block-&gt;getItemHtml($_item) ?&gt;
              &lt;/tbody&gt;
          &lt;?php endforeach; ?&gt;
      &lt;/table&gt;
   &lt;?php $_giftMessage = $this-&gt;helper('Magento\GiftMessage\Helper\Message')-&gt;getGiftMessage($_order-&gt;getGiftMessageId()); ?&gt;
          &lt;?php if ($_giftMessage): ?&gt;
              &lt;br /&gt;
              &lt;table class="message-gift"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;h3&gt;&lt;?= /* @escapeNotVerified */  __('Gift Message for this Order') ?&gt;&lt;/h3&gt;
                          &lt;strong&gt;&lt;?= /* @escapeNotVerified */  __('From:') ?&gt;&lt;/strong&gt; &lt;?= $block-&gt;escapeHtml($_giftMessage-&gt;getSender()) ?&gt;
                          &lt;br /&gt;&lt;strong&gt;&lt;?= /* @escapeNotVerified */  __('To:') ?&gt;&lt;/strong&gt; &lt;?= $block-&gt;escapeHtml($_giftMessage-&gt;getRecipient()) ?&gt;
                          &lt;br /&gt;&lt;strong&gt;&lt;?= /* @escapeNotVerified */  __('Message:') ?&gt;&lt;/strong&gt;
                          &lt;br /&gt;&lt;?= $block-&gt;escapeHtml($_giftMessage-&gt;getMessage()) ?&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;?php endif; ?&gt;
      &lt;?php endif; ?&gt;
  </code></pre>
  
  <p>Obviously, I receive this error message: </p>
  
  <blockquote>
    <p>Uncaught Error: Call to a member function getGiftMessageId() </p>
  </blockquote>
  
  <p>since my block does not have the method <code>getOrder</code>. </p>
  
  <p>But if I try to copy <code>getOrder</code> method from <code>Magento\Sales\Block\Order\Items</code>:</p>
  
  <pre class="lang-php prettyprint-override"><code>    /**
       * Retrieve current order model instance
       *
       * @return \Magento\Sales\Model\Order
       */
      public function getOrder()
      {
          return $this-&gt;_coreRegistry-&gt;registry('current_order');
      }
  </code></pre>
  
  <p>with <code>__construct</code> part</p>
  
  <pre class="lang-php prettyprint-override"><code>    /**
       * @param \Magento\Framework\View\Element\Template\Context $context
       * @param \Magento\Framework\Registry $registry
       * @param array $data
       * @param \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory|null $itemCollectionFactory
       */
      public function __construct(
          \Magento\Framework\View\Element\Template\Context $context,
          \Magento\Framework\Registry $registry,
          array $data = [],
          \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $itemCollectionFactory = null
      ) {
          $this-&gt;_coreRegistry = $registry;
          $this-&gt;itemCollectionFactory = $itemCollectionFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
              -&gt;get(\Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory::class);
          parent::__construct($context, $data);
      }
  </code></pre>
  
  <p>it still shows an error that order object in null. What am I doing wrong? Or how can I obtain order object in my case? Thanks.</p>