You are here

public static function SchemaMetatagManager::altSelector in Schema.org Metatag 8

Same name and namespace in other branches
  1. 8.2 src/SchemaMetatagManager.php \Drupal\schema_metatag\SchemaMetatagManager::altSelector()

Alternate visibility selector for the field element.

This is necessary because the form elements on the general configuration form have different parents than the form elements in the metatags field widget. This function makes is possible to convert the #states visibility selectors for the general configuration form into the right pattern so they will work on the field widget.

Parameters

string $selector: The selector constructed for the main metatag form.

Return value

string A rewritten selector that will work in the field form.

Overrides SchemaMetatagManagerInterface::altSelector

File

src/SchemaMetatagManager.php, line 358

Class

SchemaMetatagManager
Class SchemaMetatagManager.

Namespace

Drupal\schema_metatag

Code

public static function altSelector($selector) {
  $metatag_manager = \Drupal::service('metatag.manager');
  $metatag_groups = $metatag_manager
    ->sortedGroupsWithTags();
  $group = '';
  $matches = [];
  $regex = '/:input\\[name="(\\w+)\\[/';
  preg_match($regex, $selector, $matches);
  $id = $matches[1];
  foreach ($metatag_groups as $group_info) {
    if (!empty($group_info['tags'])) {
      if (array_key_exists($id, $group_info['tags'])) {
        $tag = $group_info['tags'][$id];
        $group = $tag['group'];
        break;
      }
    }
  }

  // Original pattern, general configuration form:
  // - schema_web_page_publisher[@type]
  // Alternate pattern, field widget form:
  // - field_metatags[0][schema_web_page][schema_web_page_publisher][@type].
  $original = $id . '[';
  $alternate = 'field_metatags[0][' . $group . '][' . $id . '][';
  $new = str_replace($original, $alternate, $selector);
  return $new;
}