Create and delete tags via PHP API client

Answered

3 Comments

  • Official comment
    Joey
    Comment actions Permalink

    Hi Andy-

    I checked, and this is an open issue at the moment, one workaround being the use of update_many. I understand the additional overhead for small jobs doesn't make sense, but is hopefully short-term.

    Currently, the way to achieve this using the tags CRUD functions would be to do the following instead:

    /** Zendesk\API\Traits\Resource\Update::<method>()
     * @param int $id
     * @param array $updateResourceFields
     * @param string $routeKey
     */
    $ticketTags = $client->tickets($ticketId)->tags()-><method>(null, [
        'tag_1',
        'tag_2',
    ]);

    Since the tag update endpoint does not require a tag id, null has to be passed as the first parameter. Although this usage pattern for the tags resource is indeed not intuitive, changing this in the client could be a small breaking change usage-wise. There are a few other resources that behave this way, so we'll be looking into how best to proceed with this.

    Hope this helps!

  • Andy Parkerson
    Comment actions Permalink

    I would up extending the Tags class and implementing delete there. Here's the code in case anyone stumbles across the problem and needs it.

    <?php

    /**
    * Extend the Zendesk Tags class to allow for the delete on the tickets/{id}/tags.json end point.
    *
    */

    namespace ZendeskExt;
    use Zendesk\API\Resources\Core\Tags;
    use Zendesk\API\Exceptions\MissingParametersException;
    use Zendesk\API\Exceptions\RouteException;
    use Zendesk\API\Http;

    class ScTags extends Tags
    {
    /**
    * Delete tag or group of tags
    *
    * @param mixed $params array or string of tag(s) to delete
    * @return \stdClass | null
    * @throws MissingParametersException
    */
    public function delete($params)
    {
    $chainedParameters = $this->getChainedParameters();
    if (array_key_exists(get_class($this), $chainedParameters)) {
    $id = $chainedParameters[get_class($this)];
    } else {
    $id = current($chainedParameters);
    }
    if (empty($id)) {
    throw new MissingParametersException(__METHOD__, ['id']);
    }
    try {
    $route = $this->getRoute(__FUNCTION__, ['id' => $id]);
    } catch (RouteException $e) {
    if (!isset($this->resourceName)) {
    $this->resourceName =$this->getResourceNameFromClass();
    }
    $this->setRoute(__FUNCTION__, $this->resourceName .'/{id}.json');
    $route = $this->resourceName . '/' . $id . '.json';
    }
    $response = Http::send(
    $this->client,
    $route,
    [
    'postFields'=> [$this->objectNamePlural => $params],
    'method'=>'DELETE',
    ]
    );
    return $response;
    }
    }
    Of course you have to extend the Tickets class to reference the new ScTags class. And then you have to extend the ResourceName class, to account for the different class names (if you name them differently). And then you have to extend HttpClient to call the extended Tickets. And then you have to call the extended HttpClient as ZendeskAPI instead of the original. But by then you're good.
     
    When I get a chance I'll write up some unit tests and put in a PR for it.
     
    Thanks for the help!
    1
  • Joey
    Comment actions Permalink

    Thanks, Andy. Please do consider making any PRs as you continue to work with this client.

    0

Please sign in to leave a comment.

Powered by Zendesk