/**
  * prepare the collection
  * @return $this
  */
 protected function _prepareCollection()
 {
     /** @var \Mageplaza\Blog\Model\ResourceModel\Topic\Collection $collection */
     $collection = $this->topicCollectionFactory->create();
     if ($this->getPost()->getId()) {
         $constraint = 'related.post_id=' . $this->getPost()->getId();
     } else {
         $constraint = 'related.post_id=0';
     }
     $collection->getSelect()->joinLeft(['related' => $collection->getTable('mageplaza_blog_post_topic')], 'related.topic_id=main_table.topic_id AND ' . $constraint, ['position']);
     $this->setCollection($collection);
     parent::_prepareCollection();
     return $this;
 }
 /**
  * execute action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $collection = $this->filter->getCollection($this->collectionFactory->create());
     $delete = 0;
     foreach ($collection as $item) {
         /** @var \Mageplaza\Blog\Model\Topic $item */
         $item->delete();
         $delete++;
     }
     $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $delete));
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
     return $resultRedirect->setPath('*/*/');
 }
 /**
  * @return \Mageplaza\Blog\Model\ResourceModel\Topic\Collection
  */
 public function getSelectedTopicsCollection()
 {
     if (is_null($this->topicCollection)) {
         $collection = $this->topicCollectionFactory->create();
         $collection->join('mageplaza_blog_post_topic', 'main_table.topic_id=mageplaza_blog_post_topic.topic_id AND mageplaza_blog_post_topic.post_id=' . $this->getId(), ['position']);
         $this->topicCollection = $collection;
     }
     return $this->topicCollection;
 }