Skip to main content

MarketplacePayee


MarketplacePayee

MarketplacePayee Field.

Whenever someone creates a new field in Craft, they must specify what type of field it is. The system comes with a handful of field types baked in, and we’ve made it extremely easy for plugins to add new ones.

https://craftcms.com/docs/plugins/field-types

  • Full name: \kennethormandy\marketplace\fields\MarketplacePayee
  • Parent class: Field

Methods

displayName

Returns the display name of this class.

public static displayName(): string
  • This method is static.

Return Value:

The display name of this class.


normalizeValue

public normalizeValue(mixed $value, ?\craft\base\ElementInterface $element = null): mixed

Parameters:

ParameterTypeDescription
$valuemixed
$element?\craft\base\ElementInterface

serializeValue

public serializeValue(mixed $value, ?\craft\base\ElementInterface $element = null): mixed

Parameters:

ParameterTypeDescription
$valuemixed
$element?\craft\base\ElementInterface

getInputHtml

Returns the field’s input HTML.

public getInputHtml(mixed $value, \craft\base\ElementInterface|null $element = null): string

An extremely simple implementation would be to directly return some HTML:

return '<textarea name="'.$name.'">'.$value.'</textarea>';

For more complex inputs, you might prefer to create a template, and render it via [[\craft\web\View::renderTemplate()]]. For example, the following code would render a template located at craft/plugins/myplugin/templates/_fieldinput.html, passing the $name and $value variables to it:

return Craft::$app->getView()->renderTemplate('myplugin/_fieldinput', [
'name' => $name,
'value' => $value
]);

If you need to tie any JavaScript code to your input, it’s important to know that any name= and id= attributes within the returned HTML will probably get [[\craft\web\View::namespaceInputs() namespaced]], however your JavaScript code will be left untouched.

For example, if getInputHtml() returns the following HTML:

<textarea id="foo" name="foo"></textarea>

<script type="text/javascript">
var textarea = document.getElementById('foo');
</script>

…then it might actually look like this before getting output to the browser:

<textarea id="namespace-foo" name="namespace[foo]"></textarea>

<script type="text/javascript">
var textarea = document.getElementById('foo');
</script>

As you can see, that JavaScript code will not be able to find the textarea, because the textarea’s id= attribute was changed from foo to namespace-foo.

Before you start adding namespace- to the beginning of your element ID selectors, keep in mind that the actual namespace is going to change depending on the context. Often they are randomly generated. So it’s not quite that simple.

Thankfully, [[\craft\web\View]] provides a couple handy methods that can help you deal with this:

  • [[\craft\web\View::namespaceInputId()]] will give you the namespaced version of a given ID.
  • [[\craft\web\View::namespaceInputName()]] will give you the namespaced version of a given input name.
  • [[\craft\web\View::formatInputId()]] will format an input name to look more like an ID attribute value.

So here’s what a getInputHtml() method that includes field-targeting JavaScript code might look like:

public function getInputHtml($value, $element)
{
// Come up with an ID value based on $name
$id = Craft::$app->getView()->formatInputId($name);

// Figure out what that ID is going to be namespaced into
$namespacedId = Craft::$app->getView()->namespaceInputId($id);

// Render and return the input template
return Craft::$app->getView()->renderTemplate('myplugin/_fieldinput', [
'name' => $name,
'id' => $id,
'namespacedId' => $namespacedId,
'value' => $value
]);
}

And the _fieldinput.html template might look like this:

<textarea id="{{ id }}" name="{{ name }}">{{ value }}</textarea>

<script type="text/javascript">
var textarea = document.getElementById('{{ namespacedId }}');
</script>

The same principles also apply if you’re including your JavaScript code with [[\craft\web\View::registerJs()]].

Parameters:

ParameterTypeDescription
$valuemixedThe field’s value. This will either be the [[normalizeValue() normalized value]],
raw POST data (i.e. if there was a validation error), or null
$element\craft\base\ElementInterface|nullThe element the field is associated with, if there is one

Return Value:

The input HTML.



Automatically generated on 2024-11-13