1

I'm creating a FieldType plugin, but I need the entry's ID in the front-end, I suppose I can get it within the controller, something like

craft()->entryId

$entryId = IN HERE;

$options = array(
    'coreUrl' => getenv('ENV_CORE_HOST'),
    'newEntry' => false,
    'entryId' => $entryId
);

craft()->templates->includeJs("$('#{$namespacedId}-field').whatever('" . json_encode($options) . "');");

I can get the ID in the URL with Regex and stuff, but I wouldn't like to rely on that.

And suggestion?

1

A field in Craft can belong to any element (entry, tag, category, user, etc.).

If you want to get the ID of the element the field may or may not be currently attached to, you can do this from your Field Type class:

$elementId = isset($this->element) ? $this->element->id : null;

if ($elementId)
{
    ...
}
1
  • how can I get the id in craft3 please? I created a field type and also need to get entry id that it's attached to. I tried $this->element, but got an error said "element is an unknown property"
    – yu xia
    Jun 5 '18 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.