You are here

protected function MetatagsQuickAdminSettingsForm::fieldAttach in Meta tags quick 8.3

1 call to MetatagsQuickAdminSettingsForm::fieldAttach()
MetatagsQuickAdminSettingsForm::fieldsCreateAttach in lib/Drupal/metatags_quick/MetatagsQuickAdminSettingsForm.php

File

lib/Drupal/metatags_quick/MetatagsQuickAdminSettingsForm.php, line 262
Contains Drupal\metatags_quick\MetatagsQuickAdminController.

Class

MetatagsQuickAdminSettingsForm
metatags_quick settings form parts borrowed from field_ui/FieldOverview.php

Namespace

Drupal\metatags_quick

Code

protected function fieldAttach($entity_type, $bundle, $meta_name) {

  // @todo: convert static vars to class members?
  static $meta_fields = FALSE;
  static $field_id = array();
  static $known_tags = FALSE;

  // Get metatags_quick fields info
  if (!$meta_fields) {
    include_once drupal_get_path('module', 'metatags_quick') . '/known_tags.inc';
    $known_tags = _metatags_quick_known_fields();
    foreach (field_info_fields() as $name => $field_info) {
      if ($field_info['module'] == 'metatags_quick') {
        $meta_fields[$field_info
          ->id()] = $field_info;
        $field_id[$field_info
          ->id()] = $field_info['id'];
      }
    }
  }

  // Ignore unknown tags.
  if (!isset($known_tags[$meta_name])) {
    return;
  }

  // Check if meta field exists, create if necessary.
  $meta_name_real = empty($known_tags[$meta_name]['meta_name']) ? $meta_name : $known_tags[$meta_name]['meta_name'];
  if (empty($field_id[$meta_name_real])) {
    $field = array(
      'name' => "meta_{$meta_name}",
      'type' => 'metatags_quick',
      'entity_type' => $entity_type,
    );

    // Create the field.
    $field = $this->entityManager
      ->getStorageController('field_entity')
      ->create($field);
    $field
      ->save();
    $field_id[$meta_name] = $field
      ->id();
    $meta_fields[$meta_name_real] = $field;
  }
  else {
    $field = $meta_fields[$meta_name_real];
  }

  // Do nothing if instance already exists.
  if (isset($field['bundles'][$entity_type]) && in_array($bundle, $field['bundles'][$entity_type])) {
    return;
  }

  // Now create field instance attached to requested bundle
  $instance = array(
    'field_name' => $field['field_name'],
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'label' => '(Meta)' . $known_tags[$meta_name]['title'],
    'settings' => array(
      'meta_name' => $meta_name_real,
    ),
  );
  $new_instance = $this->entityManager
    ->getStorageController('field_instance')
    ->create($instance);
  $new_instance
    ->save();

  /*     $instance = array(
        'field_name' => $field['field_name'],
        'entity_type' => $entity_type,
        'bundle' => $bundle,
        'label' => '(Meta)' . $known_tags[$meta_name]['title'],
        'formatter' => 'metatags_quick_default',
        'widget' => array(
          'type' => $known_tags[$meta_name]['widget'],
          'weight' => 0,
        ),
      );
   */
  if (isset($known_tags[$meta_name]['options'])) {
    $instance['settings']['options'] = $known_tags[$meta_name]['options'];
  }
}