0

I have create a static block in magento2 admin panel. I like use graphql with alpine.js to get data of the static block.

3 Answers 3

0

As I replied in your other question here, please go through the official channels to get support for Hyvä. If you have a license you can get support via mail or Slack.

0

Here is a small example for the usage within a PHTML file. "YourBlockName" should be changed to the respective block.

<script>
   function graphqlGetCms() {
     return {
       result: '',
       loader: '<?= $escaper->escapeHtml(__('Loading...')) ?>',
       query: `{
           cmsBlocks(identifiers: "YourBlockName") {
                 items {
                   identifier
                   title
                   content
                 }
             }
         }`,
       init () {
         window.fetch('/graphql?' + new URLSearchParams({query: this.query}))
           .then(response => response.json())
           .then(result => {
             this.result = result.errors && result.errors.length > 0
               ? result.errors.map(e => e.message).join("n")
               : result.data;
             });
       }
     };
   }
</script>
<div x-data="graphqlGetCms()">
   <template x-if="!result">
      <div x-text="loader"></div>
   </template>
   <template x-if="result">
      <template x-for="block in result.cmsBlocks.items">
         <div x-html="block.content"></div>
      </template>
   </template>
</div>

You can also load multiple blocks with this

cmsBlocks(identifiers: ["YourBlockName","YourBlockName2"])

This is only a functional example

In some cases it may also be necessary to apply contentUpdated within the template. For example, when a jquery ajax add to cart is used.

<script>    
   require(['jquery'], function($) {
        $('body').trigger('contentUpdated');
   });  
</script> 
0

You can check this theme, they use alpineJS and GraphQl https://github.com/hyva-themes

Learn from the code and apply into your project.

https://docs.hyva.io/

1
  • I have used hyva theme but I have static block to be display in home page with graphql and alpine.js .so how this is possible. Commented Mar 18, 2021 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.