# $note-\>text()-\>toBlocks()-\>excerpt(280) does not output what i expected

**URL:** <https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840>\
**Category:** Questions\
**Tags:** v3\
**Created:** [June 28, 2022, 7:56pm UTC](https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840 "2022-06-28T19:56:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://forum.getkirby.com/u/speerwerfer)\
**Post date:** [June 28, 2022, 7:56pm UTC](https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840/1 "2022-06-28T19:56:12Z")

</div>

OK, i have writen my first plugin. I can display with the plugin a leaflet map with an gpx track.  
The bleuprint of the page contains this field:

```auto
    fields:
      subheading:
        type: text
      tripname:
        label: Reise
        type: select
        options: query
        query: site.children.listed.template('trips').children
      text:
        type: blocks
        fieldsets:
          text:
            label: Text
            type: group
            fieldsets:
              - heading
              - text
              - list
              - quote
          additional:
            label: Zusatz
            type: group
            fieldsets:
              - line
              - gpxtrackleaflet
          media:
            label: Media
            type: group
            fieldsets:              
              - image
              - gallery
              - video

```

gpxtrackleaflet is the block of my plugin.

This is the PHP Code from gpxtrackleaflet.php. This shows the map with the gpx track.

```auto
<?php if($block->gpxtrack()->isNotEmpty()): ?>
    <div class="wrapper">
        <div class="kirby-leafleat-map">
            <div id="map"></div>
        </div>

    <script>
        if (map != undefined) { map.remove(); } 
        var map = L.map('map').setView([51.505, -0.09], 13);

        var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            maxZoom: 19,
            attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
        }).addTo(map);

        var gpx = '<?= $block->gpxtrack()->toFile()->url() ?>'; // URL to your GPX file or the GPX itself
        new L.GPX(gpx, {
            async: true,
            marker_options: {
                startIconUrl: '/assets/js/leaflet-gpx/pin-icon-start.png',
                endIconUrl: '/assets/js/leaflet-gpx/pin-icon-end.png',
                shadowUrl: '/assets/js/leaflet-gpx/pin-shadow.png'
            }
        }).on('loaded', function(e) {
        map.fitBounds(e.target.getBounds());
        }).addTo(map);
    </script>    
    </div>
<?php endif; ?>

```

now, when i use $note-\>text()-\>toBlocks()-\>excerpt(280) to output an excerpt of the page, and the first block of the page is an gpx track, i can see this - that’s the content of the script in my gpxtrackleaflet.php file

 ![Auswahl_023](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/2/2ed8381e43174794cb294c14d7a0958ee8f9a43d.jpeg)

Can i control the output? The excerpt should be created from the first block of text

Martin

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 28, 2022, 8:19pm UTC](https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840/2 "2022-06-28T20:19:24Z")

</div>

You can filter your blocks by type, grab the first and create the excerpt from that:

```auto
$note->text()->toBlocks()->filterBy('type', 'text')->first()->excerpt(280);

```

---

<div class="post-metadata">

**Author:** ![speerwerfer](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@speerwerfer](https://forum.getkirby.com/u/speerwerfer)\
**Post date:** [June 29, 2022, 7:37am UTC](https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840/3 "2022-06-29T07:37:05Z")

</div>

argh, ok the solution is simple. I’ll try it tonight.  
Sorry, could I have found that in the documentation myself?  
Thank you for your quick response. I get the feeling you’re on the forum 24/7 to help us.

Martin

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 29, 2022, 6:15pm UTC](https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840/4 "2022-06-29T18:15:28Z")

</div>

> [@speerwerfer](#):
>
> Sorry, could I have found that in the documentation myself?

Well, yes, but not as an answer to such an isolated question. It needs some looking in multiple places.

1. I want to get only text blocks =\> ok, I need a way to filter them, how can I do that, let’s look at the blocks object.
2. I want to get only the first item =\> ok, I need to limit the output, again what methods does the blocks object have there for me
3. Then last step, apply the excerpt.

---

<div class="post-metadata">

**Author:** ![aba3aba](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@aba3aba](https://forum.getkirby.com/u/aba3aba)\
**Post date:** [March 11, 2023, 12:10pm UTC](https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840/5 "2023-03-11T12:10:55Z")

</div>

I am facing a similar problem but the proposed solution seems to not be working for me.

I just created a field of type blocks:

```auto
fields:
        type: fields
        fields:
          text:
            type: blocks
            fieldsets:
              - text
              - image

```

And I am trying to get an excerpt from the first text

```auto
$item->text()->toBlocks()->filterBy('type', 'text')->first();

```

The line above properly return the first text block from the field.

```auto
$item->text()->toBlocks()->filterBy('type', 'text')->first()->excerpt(80);

```

But when I add the excerpt function it doesn’t work saying that it is calling to a function on null.

Am I missing any step?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 11, 2023, 12:40pm UTC](https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840/6 "2023-03-11T12:40:49Z")

</div>

Giving that you seem to be in a loop, if one of the pages doesn’t have a text blocks, this will indeed fail with the given error message.

Use the null-safe operator in this case instead:

```auto
$item->text()->toBlocks()->filterBy('type', 'text')->first()?->excerpt(80);

```

Or wrap the code in an if statement, making sure you have a field before calling excerpt.

---

<div class="post-metadata">

**Author:** ![aba3aba](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@aba3aba](https://forum.getkirby.com/u/aba3aba)\
**Post date:** [March 11, 2023, 12:56pm UTC](https://forum.getkirby.com/t/note-text-toblocks-excerpt-280-does-not-output-what-i-expected/25840/7 "2023-03-11T12:56:12Z")

</div>

Ah you were completely right. It worked perfectly, Thank you!
