Delete fieldentry from frontend?

Hi,
how can I delete a fieldentry from frontend?

It’s really easy to change some with $page->update()but what is the opposite method?
I found only $page->delete() but this is deleting a whole page.

Cheers

To delete a field from the content file:

$page->update([
  'intro' =>  null
]);

To delete only the value while keeping the field:

$page->update([
  'intro' =>  ''
]);
1 Like

Hi Sonja,
First thanks for that.
But after I restructured my blueprint I decided to use a structurefield. Now it will be a more difficult situation to delete a entry.
I add the new structureentries with

function addToStructure($page, $field, $data = array()){
    $fieldData = page($page)->$field()->yaml();
    $fieldData[] = $data;
    $fieldData = yaml::encode($fieldData);

    try {
        $page->update(array($field => $fieldData));
        return true;
    } catch(Exception $e) {
        return $e->getMessage();
    }
}

addToStructure($page, 'timetrack_entries', array(…);

How can I identify now directly a structure entry to update the fields to null?
I use also your hash_id-Plugin. Eventually I can use this for finding the right entry inside the structurefield?

You can use some code like this:

function findItem($array, $field, $value) {
   foreach($array as $key => $item) {
      if ($item[$field] === $value ) return $key;
   }
   return false;
}


$items= $page->{$field}()->yaml();

$itemKey = findItem($items, 'title', 'Contact');

unset($items[$itemKey]);

dump($items);

// then update the page with the new item collection (i.e. without the removed item)

$data = yaml::encode($items);
try {
        $page->update([$field => $data]);
        return true;
    } catch(Exception $e) {
        return $e->getMessage();
    }

Please note that with this code, you only find the first occurrence of an item that fulfils the condition. If you have multiple items, you would have to adapt the code. Or use the hash_id because that is unique.