public function GoogleAnalyticsCounterCustomFieldGenerator::gacAddField in Google Analytics Counter 8.3
Adds the checked the fields.
Parameters
\Drupal\node\NodeTypeInterface $type: A node type entity.
string $label: The formatter label display setting.
Return value
\Drupal\Core\Entity\EntityInterface|\Drupal\field\Entity\FieldConfig|null
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
\Drupal\Core\Entity\EntityStorageException
Overrides GoogleAnalyticsCounterCustomFieldGeneratorInterface::gacAddField
1 call to GoogleAnalyticsCounterCustomFieldGenerator::gacAddField()
- GoogleAnalyticsCounterCustomFieldGenerator::gacPreAddField in src/
GoogleAnalyticsCounterCustomFieldGenerator.php - Prepares to add the custom field and saves the configuration.
File
- src/
GoogleAnalyticsCounterCustomFieldGenerator.php, line 120
Class
- GoogleAnalyticsCounterCustomFieldGenerator
- Defines the Google Analytics Counter custom field generator.
Namespace
Drupal\google_analytics_counterCode
public function gacAddField(NodeTypeInterface $type, $label = 'Google Analytics Counter') {
// Check if field storage exists.
$config = FieldStorageConfig::loadByName('node', 'field_google_analytics_counter');
if (!isset($config)) {
// Obtain configuration from yaml files
$config_path = 'modules/contrib/google_analytics_counter/config/optional';
$source = new FileStorage($config_path);
// Obtain the storage manager for field storage bases.
// Create the new field configuration from the yaml configuration and save.
\Drupal::entityTypeManager()
->getStorage('field_storage_config')
->create($source
->read('field.storage.node.field_google_analytics_counter'))
->save();
}
// Add the checked fields.
$field_storage = FieldStorageConfig::loadByName('node', 'field_google_analytics_counter');
$field = FieldConfig::loadByName('node', $type
->id(), 'field_google_analytics_counter');
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $type
->id(),
'label' => $label,
'description' => t('This field stores Google Analytics pageviews.'),
'field_name' => 'field_google_analytics_counter',
'entity_type' => 'node',
'settings' => array(
'display_summary' => TRUE,
),
]);
$field
->save();
// Assign widget settings for the 'default' form mode.
entity_get_form_display('node', $type
->id(), 'default')
->setComponent('google_analytics_counter', array(
'type' => 'int',
'#maxlength' => 255,
'#default_value' => 0,
'#description' => t('This field stores Google Analytics pageviews.'),
))
->save();
// Assign display settings for the 'default' and 'teaser' view modes.
entity_get_display('node', $type
->id(), 'default')
->setComponent('google_analytics_counter', array(
'label' => 'hidden',
'type' => 'int',
))
->save();
// The teaser view mode is created by the Standard profile and therefore
// might not exist.
$view_modes = \Drupal::entityManager()
->getViewModes('node');
if (isset($view_modes['teaser'])) {
entity_get_display('node', $type
->id(), 'teaser')
->setComponent('google_analytics_counter', array(
'label' => 'hidden',
'type' => 'textfield',
))
->save();
}
}
return $field;
}