Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InlineEdit.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Cms\Api\BlockRepositoryInterface as BlockRepository;
12 
14 {
20  const ADMIN_RESOURCE = 'Magento_Cms::block';
21 
25  protected $blockRepository;
26 
30  protected $jsonFactory;
31 
37  public function __construct(
38  Context $context,
39  BlockRepository $blockRepository,
41  ) {
42  parent::__construct($context);
43  $this->blockRepository = $blockRepository;
44  $this->jsonFactory = $jsonFactory;
45  }
46 
51  public function execute()
52  {
54  $resultJson = $this->jsonFactory->create();
55  $error = false;
56  $messages = [];
57 
58  if ($this->getRequest()->getParam('isAjax')) {
59  $postItems = $this->getRequest()->getParam('items', []);
60  if (!count($postItems)) {
61  $messages[] = __('Please correct the data sent.');
62  $error = true;
63  } else {
64  foreach (array_keys($postItems) as $blockId) {
66  $block = $this->blockRepository->getById($blockId);
67  try {
68  $block->setData(array_merge($block->getData(), $postItems[$blockId]));
69  $this->blockRepository->save($block);
70  } catch (\Exception $e) {
71  $messages[] = $this->getErrorWithBlockId(
72  $block,
73  __($e->getMessage())
74  );
75  $error = true;
76  }
77  }
78  }
79  }
80 
81  return $resultJson->setData([
82  'messages' => $messages,
83  'error' => $error
84  ]);
85  }
86 
94  protected function getErrorWithBlockId(BlockInterface $block, $errorText)
95  {
96  return '[Block ID: ' . $block->getId() . '] ' . $errorText;
97  }
98 }
__construct(Context $context, BlockRepository $blockRepository, JsonFactory $jsonFactory)
Definition: InlineEdit.php:37
__()
Definition: __.php:13
$block
Definition: block.php:8
getErrorWithBlockId(BlockInterface $block, $errorText)
Definition: InlineEdit.php:94