Commit 0013e729 authored by kedramon's avatar kedramon Committed by Robert Ragas

Issue #2573897 by kedramon: Multiple field support

parent ec57a980
<?php
/**
* @file yoast.forms.inc
* Contains node form alters.
*/
/**
* Adds extra settings to the node content type edit page.
*
* @param $form
* Node settings form.
*/
function _yoast_seo_process_node_settings_form(&$form) {
// Do not process node form if it do not contain node type.
if (isset($form['type'])) {
$form['yoast_seo'] = array(
'#type' => 'fieldset',
'#title' => t('Yoast SEO for Drupal settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
$node_type = $form['#node_type']->type;
// Get the fields belonging to this content type. Let's check if we have a
// body field. This is necessary for Yoast SEO for Drupal to work.
$fields = field_info_instances('node', $node_type);
// Collect all text fields.
$text_fields = array(0 => t('- None -'));
foreach ($fields as $field_name => $field_instance) {
if ($field_name === 'body') {
continue;
}
if ($field_instance['widget']['module'] === 'text') {
$text_fields[$field_name] = $field_instance['label'] . ' (' . $field_name . ')';
}
}
/**
* @todo remove this after https://www.drupal.org/node/1149078 was fixed and use
* $states = array(
* 'disabled' => array(
* ':input[name=yoast_seo_body_fields[]]' => array('value' => array('0')),
* ),
* );
*/
$conditions = array();
unset($text_fields[0]);
$field_names = array_keys($text_fields);
foreach ($field_names as $label) {
$conditions[][':input[name="yoast_seo_body_fields[' . $label . ']"]'] = array('checked' => TRUE);
}
$body_fields = variable_get('yoast_seo_body_fields_' . $node_type, array());
if (!isset($fields['body']) && !empty($node_type) && empty($body_fields)) {
$default = FALSE;
$description = t('Yoast SEO for Drupal cannot be enabled, because this content type does not have a body field. Add a body field !url.', array('!url' => l(t('here'), 'admin/structure/types/manage/' . $form['#node_type']->type . '/fields')));
$disabled = TRUE;
$states = array(
'enabled' => array(
$conditions
),
);
}
else {
if (isset($fields['body'])) {
// if we have body we skip conditions
$conditions = array();
}
$default = variable_get('yoast_seo_enable_node__' . $node_type, TRUE);
$description = t('Enable Yoast SEO for Drupal for this content type.');
$disabled = FALSE;
$states = array(
'enabled' => array(
$conditions
),
);
}
$form['yoast_seo']['yoast_seo_enable_node_'] = array(
'#type' => 'checkbox',
'#title' => t('Enable'),
'#default_value' => $default,
'#disabled' => $disabled,
'#description' => $description,
'#states' => $states,
);
/**
* Here we allow users to use different fields or multiple for using instead of body.
* @todo change this to multiselect after https://www.drupal.org/node/1149078 was fixed
* '#type' => 'select',
* '#multiple' => TRUE,
*/
if (empty($body_fields)) {
$description = t('There is no filed that can be used as additional field.');
}
else {
$description = t('Select fields to use instead or in addition to body for Yoast SEO. Access to all fields must be allowed.');
}
$form['yoast_seo']['yoast_seo_body']['yoast_seo_body_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Additional body source'),
'#description' => $description,
'#options' => $text_fields,
'#default_value' => $body_fields,
);
}
}
......@@ -87,15 +87,18 @@
if (typeof CKEDITOR !== "undefined") {
CKEDITOR.on('instanceReady', function( ev ) {
var editor = ev.editor;
// Check if this the instance we want to track.
if (editor.name == YoastSEO.analyzerArgs.fields.text) {
editor.on('change', function() {
// Let CKEditor handle updating the linked text element.
editor.updateElement();
// Dispatch input event so Yoast SEO knows something changed!
DrupalSource.triggerEvent(editor.name);
});
for (var text_field in YoastSEO.analyzerArgs.fields.text) {
// Check if this the instance we want to track.
if (typeof YoastSEO.analyzerArgs.fields.text[text_field] != 'undefined') {
if (editor.name == YoastSEO.analyzerArgs.fields.text[text_field]) {
editor.on('change', function() {
// Let CKEditor handle updating the linked text element.
editor.updateElement();
// Dispatch input event so Yoast SEO knows something changed!
DrupalSource.triggerEvent(editor.name);
});
}
}
}
});
}
......@@ -185,15 +188,31 @@ YoastSEO_DrupalSource.prototype.getData = function() {
};
YoastSEO_DrupalSource.prototype.getDataFromInput = function( field ) {
return document.getElementById(this.config.fields[field]).value;
}
// If this is an array of id's
if (this.config.fields[field] instanceof Array) {
var output = [];
for (var text_field in this.config.fields[field]) {
if (
typeof this.config.fields[field][text_field] != 'undefined'
&& document.getElementById(this.config.fields[field][text_field])
&& document.getElementById(this.config.fields[field][text_field]).value != ''
) {
output.push(document.getElementById(this.config.fields[field][text_field]).value);
}
}
return output.join("\n");
}else{
return document.getElementById(this.config.fields[field]).value;
}
};
/**
* Grabs data from the refObj and returns populated analyzerData
* @returns analyzerData
*/
YoastSEO_DrupalSource.prototype.updateRawData = function() {
data = {
var data = {
keyword: this.getDataFromInput( "keyword" ),
meta: this.getDataFromInput( "meta" ),
snippetMeta: this.getDataFromInput( "meta" ),
......@@ -238,6 +257,14 @@ YoastSEO_DrupalSource.prototype.bindElementEvents = function() {
*/
YoastSEO_DrupalSource.prototype.inputElementEventBinder = function() {
for (field in this.config.fields) {
if (this.config.fields[field] instanceof Array) {
for (var text_field in this.config.fields[field]) {
if (typeof this.config.fields[field][text_field] != 'undefined' && document.getElementById(this.config.fields[field][text_field])) {
document.getElementById(this.config.fields[field][text_field]).__refObj = this;
document.getElementById(this.config.fields[field][text_field]).addEventListener("input", this.renewData.bind(this));
}
}
}
if (typeof this.config.fields[field] != 'undefined' && document.getElementById(this.config.fields[field])) {
document.getElementById(this.config.fields[field]).__refObj = this;
document.getElementById(this.config.fields[field]).addEventListener("input", this.renewData.bind(this));
......@@ -311,9 +338,7 @@ YoastSEO_DrupalSource.prototype.scoreRating = function (rating) {
scoreRate = "na";
}
var output = Drupal.t("SEO: <strong>" + scoreRate + "</strong>");
return output;
return Drupal.t("SEO: <strong>" + scoreRate + "</strong>");
};
/**
......
......@@ -107,6 +107,7 @@ function yoast_seo_uninstall() {
if (!empty($entity_info['bundles'])) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
variable_del('yoast_seo_enable_' . $entity_type . '__' . $bundle_name);
variable_del('yoast_seo_body_fields_' . $bundle_name);
}
}
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment