Advertising sustains the DA. Ads are hidden for members. Join today

Metainfo mapping

Last updated on
27 April 2017

As Linkbacks can trigger the fetching of additional metainfo from linking source, some mapping can be done to "convert" properties in metainfo field to user defined fields. In Linkback, webmention component submodule stores in metainfo field serialized as json, the parsing of basic rdf (schema.org) and microformats2. 

To carry on the conversion from metainfo property to Linkback entity field, every Linkback text and link field created by user can be mapped in field settings ("admin/structure/linkback/manage/fields" > edit) in linkback structure ( admin/structure/linkback) to a json property stored in metainfo. By now rdf mapping and microformat mapping can be done. When Linkback is presaved it will create the values in every field that has some mapping defined.

A thirdparty options on each linkback field will bring options to get the value of json object to set the field.

To add new mapping you can implement in a new submodule. One need to add a new schema to specify the thirdpartysettings that a field can use in config/schema/mymodule.schema.yml :

field.field.linkback.*.*.third_party.linkback:
type: mapping
label: Linkback mapping
mapping:
  new_mapping:
    type: string
    label: 'New mapping'

Then you must alter the field config form reimplementing hook_form_FORM_ID_form_alter for field_config_edit form, optionally restrict to the specified allowed types:

/**
* Implements hook_form_FORM_ID_form_alter().
*/
function mymodule_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {
  /** @var \Drupal\Core\Field\FieldConfigInterface $field */
  $field = $form_state->getFormObject()->getEntity();
  $allowed_types = [
    'string',
    'string_long',
    'text',
    'text_long',
    'text_with_summary',
    'link',
  ];
  if (in_array($field->getType(), $allowed_types)) {
    $form['third_party_settings']['linkback']['new_mapping'] = [
      '#type' => 'textfield',
      '#title' => t('Mapped new property'),
      '#placeholder' => 'author/thumbnail',
      '#default_value' => $field->getThirdPartySetting('mymodule', 'new_mapping'),
      '#description' => t('If the key defined is in the parsed metainfo json object,
      the pointed value will be defined in this field when processing linkback.The property
      must be separated by "/" if any depth is needed.'),
    ];
  }
}

With these two instructions; if you create a new metainfo json structure (that will be stored in linkback field metainfo), specifying parser "new" linkback automatically will fill the field with the pointed property value when linkback entity is saved.

Help improve this page

Page status: No known problems

You can: