• Resolved mcsr

    (@mcsr)


    Hi there,

    I’m trying to edit this field using the WordPress REST-API, but get the error acf[table] ist nicht vom Typ string,null

    This is the body of my POST request:

    {
    "acf": {
    "table": {
    "use_header": true,
    "header": [
    {
    "c": "Header Row 1 Col A"
    },
    {
    "c": "Header Row 1 Col B"
    }
    ],
    "caption": false,
    "body": [
    [
    {
    "c": "Body Row 1 Col A"
    },
    {
    "c": "Body Row 1 Col B"
    }
    ],
    [
    {
    "c": "Body Row 2 Col A"
    },
    {
    "c": "Body Row 2 Col B"
    }
    ]
    ]
    }
    }
    }

    How can I update the field using the REST-API?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Johann Heyne

    (@jonua)

    Hi,

    the last version of the plugin 1.3.23 should fix the REST-API error “string,null“.

    For updating the table object via REST-API you first should get the unformatted table data.

    In PHP you have to set the 3 parameter of get_field() to false.
    $unformatted_table_data = get_field( 'fieldname', $post_id, false);

    In JavaScript you can get the unformatted table data by the REST-API.

    Then you can modify the table data object and update the field by the REST-API.

    The unformatted table object of a field named “table” of an REST-API GET should look like this example:

    {
    "acf": {
    "table": {
    "acftf": {
    "v": "1.3.21"
    },
    "p": {
    "o": {
    "uh": 1
    },
    "ca": "Table caption content"
    },
    "c": [
    {
    "p": ""
    },
    {
    "p": ""
    }
    ],
    "h": [
    {
    "c": "Header, first cell content"
    },
    {
    "c": "Header, second cell content"
    }
    ],
    "b": [
    [
    {
    "c": "Body Row 1, first cell content"
    },
    {
    "c": "Body Row 1, second cell content"
    }
    ],
    [
    {
    "c": "Body Row 2, first cell content"
    },
    {
    "c": "Body Row 2, second cell content"
    }
    ]
    ]
    }
    }
    }

    acf.table.p This object contains table parameters.
    acf.table.p.o This object contains table options.
    acf.table.p.o.uh Determines whether a header should be used or not.
    acf.table.p.ca The content of the table caption.

    acf.table.c Array of table columns. Amount should always be the same as in the table header or body row arrays. This columns array was intended to hold columns parameters.
    acf.table.c[0].p First column, still unused parameter property.

    acf.table.h Array of the table header cells.
    acf.table.h[0].c Content of the first header cell.

    acf.table.b Array of the table body rows.
    acf.table.b[0] Array of the cells of the table body first row.
    acf.table.b[0][0].c Content of the cell of the table body first cell in first row.

    I hope this helps.
    Cheers, Johann

    • This reply was modified 4 months, 3 weeks ago by Johann Heyne.
    • This reply was modified 4 months, 3 weeks ago by Johann Heyne.
    Thread Starter mcsr

    (@mcsr)

    Hi @jonua ,

    Thanks for the update and detailed explanation! Now it’s working fine.

    I’ve noticed another thing where I’m not sure if it’s intended like this: When the table is empty, the field returns false on the REST-API. Shouldn’t this be null instead of false ?

    Plugin Author Johann Heyne

    (@jonua)

    Thanks! You’re right. The plugin returns false if there is no table entry. I overlooked this setting up the Rest-API schema and fixed it with the 1.3.24 version. The Rest-API should still return false on empty table.

    • This reply was modified 4 months, 3 weeks ago by Johann Heyne.
    Thread Starter mcsr

    (@mcsr)

    The REST-API now returns false if the table is empty, that’s correct. Is there a reason why it doesn’t return null as other empty ACF fields?

    Plugin Author Johann Heyne

    (@jonua)

    I thought it might be helpful to differentiate between the following two scenarios:

    If the table has only one empty cell, then?get_field()?returns?FALSE.?get_field()?returns NULL when a field is not stored in the database. That happens when a page is copied but not their fields content. You can check both with?empty()

    $table = get_field( 'your_table_field_name' );
    
    if ( ! empty( $table ) ) {
        // $table is not FALSE and not NULL.
        // Field exists in database and has content.
    }

    Is this inconsistency compared to other fields a problem?

    See docs

    Thread Starter mcsr

    (@mcsr)

    I didn’t know about the behavior of get_field(), but it makes sense to adopt this to the table field. Maybe I was a bit confused because we used it most of the time with GraphQL and there empty fields return null .

    I’m fine with both false and null

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.