Creating your own fieldtype
DataGrab has the ability to register custom 3rd party fieldtypes.
To add a custom fieldtype add add the folder "datagrab_fieldytpes" in the addons directory at the same level as your "datagrab" add-on folder. For example:

If you create a file with the same name as an existing file in the datagrab/fieldtypes directory, it will be loaded first, effectively giving you the ability to override core DataGrab functionality. With great power comes great responsibility. While we provide the ability to do this, we do not offer support for debugging or assisting creating these override files. You're on your own should you choose this path.
Your datatype class will need to extend the AbstractDataType class. At minimum it will need to implement a few methods. There are a lot of examples to follow if you refer to the files within the datagrab/fieldtypes
folder.
<?php
use BoldMinded\DataGrab\FieldTypes\AbstractFieldType;
use BoldMinded\DataGrab\FieldTypes\ImportField;
use BoldMinded\DataGrab\Service\Importer;
class Datagrab_my_fieldtype extends AbstractFieldType
{
// Should return an array with the fieldName as the key.
// Each item in the array is an option name used when
// configuring the field for import. You should have fields
// with the same names rendered in your display_configuration method.
public function register_setting(
string $fieldName
) {
return [
$fieldName => [
'option_one',
'option_two',
]
];
}
public function display_configuration(
Importer $importer,
string $fieldName,
string $fieldLabel,
string $fieldType,
bool $fieldRequired = false,
array $data = []
): array {
}
// This is called to render the field settings if this
// field is a child of a Grid, Fluid, or Bloqs field.
// Generally best practice is to use this method, and then
// call it within your own display_configuration()
public function getFormFields(
string $fieldName,
array $fieldSettings,
array $data = [],
array $savedFieldValues = [],
string $contentType = 'grid',
Importer $importer = null
): array {
}
// Optional, only necessary if you have some pre-processing
// to do before the field is saved during the import process.
public function preparePostData(
ImportField $importField
): string
{
}
// Optional, only necessary if you have some post-processing
// to do before the field is saved during the import process.
public function finalPostData(
ImportField $importField
): array
{
}
// Optional, only necessary if you have a complicated fieldtype
// similar to Bloqs, Grid, or Fluid. This should iterate all
// possible import rows/items and if there is a value in any of them
// return the first. The return value does not matter, it just
// has to return something. If it returns a blank string the importer
// will not enact upon the field. Based on the function name you'd
// expect this to return a boolean, but for reasons it returns a string.
public function hasFieldValue(
array $settings = []
): string
{
}
}
<?php
// This is for DataGrab version 5, which is deprecated.
// Please upgrade to DataGrab 6 and use the example above.
class Datagrab_my_fieldtype extends AbstractFieldType
{
public function register_setting(
string $fieldName
) {
}
public function display_configuration(
Datagrab_model $DG,
string $fieldName,
string $fieldLabel,
string $fieldType,
bool $fieldRequired = false,
array $data = []
): array {
}
// Optional
public function final_post_data(
Datagrab_model $DG,
array $item = [],
int $fieldId = 0,
string $fieldName = '',
array &$data = [],
int $updateEntryId = 0
) {
}
// Optional
public function prepare_post_data(
Datagrab_model $DG,
array $item = [],
int $fieldId = 0,
string $fieldName = '',
array &$data = [],
int $updateEntryId = 0
) {
}
}
Last updated
Was this helpful?